-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.java
More file actions
57 lines (52 loc) · 1.05 KB
/
HeapSort.java
File metadata and controls
57 lines (52 loc) · 1.05 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
56
57
public class HeapSort {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] A = {5, 4, 9, 16, 14, 11, 12, 8, 7, 2, 3, 1};
heaplify(A, A.length - 1);
int end = A.length - 1;
print(A);
while(end > 0){
swap(A, 0, end);
shiftDown(A, 0, --end);
print(A);
}
print(A);
}
public static void print(int[] A){
for(int val: A)
System.out.print(val + ",");
System.out.println();
}
public static void heaplify(int[] A, int n){
//Not a full heap
//n = (n - 2) / 2;
for(int i = n; i >= 0; --i)
shiftDown(A, i, n);
}
public static void shiftDown(int[] A, int root, int end){
while(root * 2 + 1 <= end){
int child = root * 2 + 1;
int swap = root;
if(A[swap] < A[child]){
swap = child;
}
if(child + 1 <= end && A[swap] < A[child + 1]){
swap = child + 1;
}
if(swap != root){
swap(A, swap, root);
root = swap;
}
else
break;
}
}
public static void swap(int[] A, int i, int j){
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
}