public class Heapsort
extends java.lang.Object
Heapsort
Java application illustrates a heapsort.
Part of the implementation (the makeHeap
and
reheapifyDown
methods) is left
as a student exercise.
Java Source Code for this class (without
makeHeap
and reheapifyDown
:
http://www.cs.colorado.edu/~main/applications/Heapsort.java
Constructor and Description |
---|
Heapsort() |
Modifier and Type | Method and Description |
---|---|
static void |
heapsort(int[] data,
int n)
This method cannot be used until the student implements
makeHeap and reheapifyDown . |
static void |
main(java.lang.String[] args)
The main method illustrates the use of a heapsort to sort a
small array.
|
public static void main(java.lang.String[] args)
args
- not used in this implementationpublic static void heapsort(int[] data, int n)
makeHeap
and reheapifyDown
.
Sort an array of integers from smallest to largest, using a heapsort
algorithm.data
- the array to be sortedn
- the number of elements to sort, (from data[0]
through data[n-1]
)
Precondition:
data
has at least n
elements.
Postcondition:
If n
is zero or negative then no work is done. Otherwise,
the elements of data
have been rearranged so that
data[0] <= data[1] <= ... <= data[n-1]
.java.lang.ArrayIndexOutOfBoundsException
- Indicates that data
has fewer than n
elements.