-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.java
More file actions
58 lines (45 loc) · 1.28 KB
/
HeapSort.java
File metadata and controls
58 lines (45 loc) · 1.28 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
/**
* Created by Phil on 8/15/2015.
*/
public class HeapSort<Key> extends MyMaxPQ<Key> {
@Override
public Key deleteMax() {
if(size == 0) {
System.out.println("empty PQ!");
return null;
}
Key retVal = heap[1];
heap[1] = heap[size--];
heap[size+1] = retVal;
sink(1);
return null;
}
public void sort() {
int heapSize = size;
for(int j = 0; j < heapSize; j++)
deleteMax();
}
public static void main(String[] args) {
HeapSort heapSort = new HeapSort();
heapSort.insert(12);
heapSort.insert(5);
heapSort.insert(3);
heapSort.insert(21);
heapSort.insert(1);
heapSort.insert(8);
heapSort.insert(11);
heapSort.insert(2);
heapSort.insert(6);
heapSort.insert(15);
heapSort.insert(4);
for(int i = 0; i < heapSort.heap.length; i++) {
if(heapSort.heap[i] != null) System.out.print(heapSort.heap[i] + " ");
}
System.out.println();
heapSort.sort();
for(int i = 0; i < heapSort.heap.length; i++) {
if(heapSort.heap[i] != null) System.out.print(heapSort.heap[i] + " ");
}
System.out.println();
}
}