-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheapsort.java
More file actions
29 lines (29 loc) · 809 Bytes
/
heapsort.java
File metadata and controls
29 lines (29 loc) · 809 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*Heapsort:java implementation*/ /*not stable, heap construction <=2N compares and exchanges. heapsort <=2NlgN compares and exchanges*/
public class Heap {
public static void sort (Comparable[] a) {
int N = a.length;
for(int k = N/2; k >= 1 ; k--)
sink(a, k, N);
while (N > 1) {
exch(a, 1, N);
sink(a, 1, --N);
}
}
private static void sink(Comparable[] a, int k, int N) {
while (2*k <= N) {
int j = 2*k;
if(j < N && less (j, j+1)) j++;
if (!less(k,j)) break;
exch(k,j);
k = j;
}
}
private boolean less( Comparable[] a, int i, int j) { //convert from 1-basedindexing to 0-based indexing
return a[i-1].compareTo(a[j-1] < 0;
}
private void exch ( Comparable[] a, int i, int j) {
Key t = a[i-1]; // Key is the data type of a[]
a[i-1] = a[j-1];
a[j-1] = t;
}
}