-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.java
More file actions
55 lines (44 loc) · 1.22 KB
/
HeapSort.java
File metadata and controls
55 lines (44 loc) · 1.22 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class HeapSort {
private static Comparable[] array = {2,8,4,7,1,5,9,3,6};
public static void main(String[] args) {
HeapSort hSort = new HeapSort();
hSort.sort(array);
System.out.println(Arrays.toString(array));
}
public void sort(Comparable[] array) {
int n = array.length;
buildMaxHeap(array, n);
sortDown(array, n);
}
private void buildMaxHeap(Comparable[] a, int n) {
for (int k = n / 2; k >= 1; k--){
sink(a, k, n);
}
}
private void sortDown(Comparable[] a, int n) {
while (n > 1) {
swap(a, 1, n--);
sink(a, 1, n);
}
}
private void sink(Comparable[] a, int k, int n) {
int j;
while (2 * k <= n) {
j = 2 * k;
if (j < n && less(a, j, j + 1)){
j++;}
if (!less(a, k, j)){
break;}
swap(a, k, j);
k = j;
}
}
private boolean less(Comparable[] a, int i, int j) {
return a[i - 1].compareTo(a[j - 1]) < 0;
}
private void swap(Object[] a, int i, int j) {
Object tmp = a[i - 1];
a[i - 1] = a[j - 1];
a[j - 1] = tmp;
}
}