-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.java
More file actions
206 lines (140 loc) · 4.88 KB
/
HeapSort.java
File metadata and controls
206 lines (140 loc) · 4.88 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package algorithm.sort;
import java.util.Random;
public class HeapSort {
public static void main(String[] args) {
int problemSize = 17;
final Random random = new Random();
int[] data = new int[]{90,38,5,55,26,76,36,11,51,2,16,64,81,1,48,9,77};
// Insert的构造方式,复杂度是O( N * LogN )
int[] maxHeap = new int[problemSize];
for(int i = 0; i < problemSize; i ++) {
insert(maxHeap, i, data[i]);
}
System.out.println(" -- Create O(N Log N) --");
printHeap(maxHeap);
System.out.println();
for(int i = problemSize -1 ; i > 0; i--) {
System.out.println(extract(maxHeap, i));
}
System.out.println();
System.out.println(" -- Create O(N) --");
heapify(data, problemSize);
printHeap(data);
System.out.println();
for(int i = problemSize -1 ; i > 0; i--) {
System.out.println(extract(data, i));
}
}
static void insert(int[] binaryHeap, int i, int value) {
// Step 1. put value at the end of binary heap
binaryHeap[i] = value;
// Step 2. keep binary heap
int end = i - 1;
if(end < 0) {
return;
}
while(i > 0 && binaryHeap[parentIndex(i)] < binaryHeap[i]){
swap(binaryHeap, parentIndex(i), i);
i = parentIndex(i);
}
}
static int extract(int[] binaryHeap, int end) {
int max = binaryHeap[0];
binaryHeap[0] = binaryHeap[end];
binaryHeap[end] = 0;
int i = 0;
while( leftChildIndex(i) <= (end - 1)) {
// Select bigger child then swap with it
int position = i;
int leftSon = leftChildIndex(i);
// First, compare with left child
if(binaryHeap[i] < binaryHeap[leftSon]) {
position = leftSon;
}
// Second, if there has right child and that child is greater
if( (leftSon + 1) <= (end -1) && binaryHeap[leftSon + 1] > binaryHeap[leftSon]) {
position = leftSon + 1;
}
// Node i holds the largest element, this means that we are done at this level
if(position == i) {
return max;
}else {
// Continue sifting down the child
swap(binaryHeap, i, position);
i = position;
}
}
return max;
}
static void heapify(int[] binarayHeap, int length) {
// 找到最后一个叶子节点的父节点,以此为起点。
int i = parentIndex(length-1);
// 每次下标减1,从而遍历整个堆,调整并保持堆的特性。
for(; i > 0; i--) {
siftDown(binarayHeap, i);
}
}
// 向下检查比较
static void siftDown(int[] binaryHeap, int index) {
int end = binaryHeap.length - 1;
while( leftChildIndex(index) <= end) {
// Select bigger child then swap with it
int position = index;
int leftSon = leftChildIndex(index);
// First, compare with left child
if(binaryHeap[index] < binaryHeap[leftSon]) {
position = leftSon;
}
// Second, if there has right child and that child is greater
if( (leftSon + 1) <= end && binaryHeap[leftSon + 1] > binaryHeap[position]) {
position = leftSon + 1;
}
// Node i holds the largest element, this means that we are done at this level
if(position == index) {
return;
}else {
// Continue sifting down the child
swap(binaryHeap, index, position);
index = position;
}
}
}
/** Help Methods **/
static int parentIndex(int n) {
return (n-1)/2;
}
static int leftChildIndex(int n) {
return 2*n + 1;
}
static int rightChildIndex(int n) {
return 2*n + 2;
}
static void swap(int[] array, int from, int to) {
int temp = array[from];
array[from] = array[to];
array[to] = temp;
}
static void printHeap(int[] heap) {
int length = heap.length;
int k = 0;
int i = 0;
// Log N Times Loop
for(; (int)Math.pow(2, i) < length; i++ ) {
System.out.print("Level " + i + " : ");
int distance = (int)Math.pow(2,i);
if((int)Math.pow(2,i + 1) > length) {
distance = length - distance + 1;
}
for(int j = 0; j < distance; j++, k++) {
System.out.print(heap[k] + " ");
}
System.out.println();
}
if(k < length) {
System.out.print("Level " + i + " : ");
for(; k < length; k++) {
System.out.print(heap[k] + " ");
}
}
}
}