-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.java
More file actions
69 lines (58 loc) · 1.97 KB
/
HeapSort.java
File metadata and controls
69 lines (58 loc) · 1.97 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
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.Comparator;
public class HeapSort<T> {
private static long comparisons = 0;
private static Object[] heap;
private static int size;
public static <T> void sort(T[] array, Comparator<T> comp) {
heap = array;
size = array.length;
comparisons = 0;
for (int i = size / 2 - 1; i >= 0; i--) {
downheap(i, comp);
}
for (int i = size - 1; i > 0; i--) {
swap(0, i);
size--;
downheap(0, comp);
}
}
private static <T> void upheap(int index, Comparator<T> comp) {
int parentIndex = (index - 1) / 2;
while (index > 0 && comp.compare((T)heap[index], (T)heap[parentIndex]) < 0) {
swap(index, parentIndex);
index = parentIndex;
parentIndex = (index - 1) / 2;
comparisons++;
}
}
private static <T> void downheap(int index, Comparator<T> comp) {
int leftChildIndex = 2 * index + 1;
int rightChildIndex = 2 * index + 2;
while (leftChildIndex < size) {
int smallerChildIndex = leftChildIndex;
if (rightChildIndex < size) {
comparisons++;
if (comp.compare((T) heap[rightChildIndex], (T) heap[leftChildIndex]) < 0) {
smallerChildIndex = rightChildIndex;
}
}
comparisons++;
if (comp.compare((T) heap[index], (T) heap[smallerChildIndex]) > 0) {
swap(index, smallerChildIndex);
index = smallerChildIndex;
leftChildIndex = 2 * index + 1;
rightChildIndex = 2 * index + 2;
} else {
break;
}
}
}
private static void swap(int index1, int index2) {
Object temp = heap[index1];
heap[index1] = heap[index2];
heap[index2] = temp;
}
public static long getComparisons() {
return comparisons;
}
}