Java Reference
In-Depth Information
Figure 22
Using Heapsort to Sort an Array
753
754
ch16/heapsort/HeapSorter.java
1 /**
2 This class applies the heapsort algorithm to sort an array.
3 */
4 public class HeapSorter
5 {
6 /**
7 Constructs a heap sorter that sorts a given array.
8 @param anArray an array of integers
9 */
10 public HeapSorter( int [] anArray)
11 {
12 a = anArray;
13 }
14
15 /**
16 Sorts the array managed by this heap sorter.
17 */
18 public void sort()
19 {
20 int n = a.length - 1 ;
21 for ( int i = (n - 1 ) / 2 ; i >= 0 ; i--)
22 fixHeap(i, n);
23 while (n > 0 )
24 {
25 swap( 0 , n);
26 n--;
Search WWH ::




Custom Search