-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHeapSort.java
More file actions
49 lines (42 loc) · 1.02 KB
/
Copy pathHeapSort.java
File metadata and controls
49 lines (42 loc) · 1.02 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
public class HeapSort
{
/* Heapsort - ont of the best sorting algorithms
* in place and 2NlgN
* but not stable
* poor cache memory usage */
public static void sort(Comparable[] a)
{
int N = a.length;
// Step 1: Create a max-heap out of the given array (O(NlgN))
for (int i = N/2; i >= 1; i--)
sink(a, i, N);
// Step 2: Repeatedly do delMax //O(NlgN)
while (N > 1)
{
exch(a, 1, N--);
sink(a, 1, N);
}
assert SortingHelpers.isSorted(a, 0, a.length-1);
}
/* Helper method */
private static void sink(Comparable[] a, int index, int N)
{
while (2*index <= N)
{
int j = 2*index;
if (j < N && less(a, j, j+1)) j++;
if (!less(a, index, j)) break;
exch(a, index, j);
index = j;
}
}
/* Override less() and exch() methods to accomodate the fact that Heap starts at index 1 */
private static boolean less(Comparable[] a, int i, int j)
{
return SortingHelpers.less(a[i-1], a[j-1]);
}
protected static void exch(Comparable[] a, int i, int j)
{
SortingHelpers.exch(a, i-1, j-1);
}
}