-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.java
More file actions
39 lines (36 loc) · 1.19 KB
/
Copy pathHeapSort.java
File metadata and controls
39 lines (36 loc) · 1.19 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
/*
* ========================= TEMPLATE =========================
* Heap Sort (max-heap, in-place) for int[]
* Copy-paste into your project and tweak as needed.
* - No package
* - Small, static API
* - Safe to rename class/methods
* - TODO: not stable. Keep when O(1) extra space matters.
* ===============================================================
*/
public class HeapSort {
private HeapSort() {}
public static void heapSort(int[] a) {
if (a == null || a.length < 2) return;
int n = a.length;
for (int i = n / 2 - 1; i >= 0; i--) siftDown(a, i, n);
for (int end = n - 1; end > 0; end--) {
swap(a, 0, end);
siftDown(a, 0, end);
}
}
private static void siftDown(int[] a, int i, int n) {
while (true) {
int left = 2 * i + 1;
if (left >= n) break;
int largest = left, right = left + 1;
if (right < n && a[right] > a[left]) largest = right;
if (a[i] >= a[largest]) break;
swap(a, i, largest);
i = largest;
}
}
private static void swap(int[] a, int i, int j) {
int t = a[i]; a[i] = a[j]; a[j] = t;
}
}