-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeapSort.java
78 lines (69 loc) · 2.17 KB
/
HeapSort.java
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
70
71
72
73
74
75
76
77
78
package sorting_searching;
import java.util.Comparator;
public class HeapSort {
public static void main(String[] args) {
// Example:
int[] array = {91, 34, 86, 39, 27, 87, 70, 48, 100, 95};
heapSort(array);
System.out.println(java.util.Arrays.toString(array)); // [27, 34, 39, 48, 70, 86, 87, 91, 95, 100]
}
public static void heapSort(int[] a) {
for (int i = a.length / 2 - 1; i >= 0; --i) {
restoreHeapCondition(a, i, a.length);
}
for (int m = a.length - 1; m >= 1; --m) {
int temp = a[0];
a[0] = a[m];
a[m] = temp;
restoreHeapCondition(a, 0, m - 1);
}
}
private static void restoreHeapCondition(int[] a, int i, int heapSize) {
while (2 * (i + 1) - 1 < heapSize) {
int j = 2 * (i + 1) - 1;
if (j + 1 < heapSize) {
if (a[j] < a[j + 1]) {
++j;
}
}
if (a[i] >= a[j]) {
return;
}
int temp = a[i];
a[i] = a[j];
a[j] = temp;
i = j;
}
}
public static <T extends Comparable<? super T>> void heapSort(T[] a) {
heapSort(a, Comparator.naturalOrder());
}
public static <T> void heapSort(T[] a, Comparator<T> comp) {
for (int i = a.length / 2 - 1; i >= 0; --i) {
restoreHeapCondition(a, i, a.length, comp);
}
for (int m = a.length - 1; m >= 1; --m) {
T temp = a[0];
a[0] = a[m];
a[m] = temp;
restoreHeapCondition(a, 0, m - 1, comp);
}
}
private static <T> void restoreHeapCondition(T[] a, int i, int heapSize, Comparator<T> comp) {
while (2 * (i + 1) - 1 < heapSize) {
int j = 2 * (i + 1) - 1;
if (j + 1 < heapSize) {
if (comp.compare(a[j], a[j + 1]) < 0) {
++j;
}
}
if (comp.compare(a[i], a[j]) >= 0) {
return;
}
T temp = a[i];
a[i] = a[j];
a[j] = temp;
i = j;
}
}
}