-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.js
48 lines (43 loc) · 1.23 KB
/
heap.js
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
const heapSort = () => {
// turn array into heap
for (let hole = Math.floor(arr.length / 2) - 1; hole >= 0; hole--) {
let holeTmp = hole;
while (holeTmp * 2 + 1 < arr.length) {
let minIndex = holeTmp * 2 + 1;
if (minIndex + 1 < arr.length && arr[minIndex + 1] < arr[minIndex])
minIndex = holeTmp * 2 + 2;
if (arr[holeTmp] > arr[minIndex]) {
let tmp = arr[holeTmp];
arr[holeTmp] = arr[minIndex];
arr[minIndex] = tmp;
holeTmp = minIndex;
} else {
break;
}
}
}
// remove min
// remove the top elemetn and replace it with the last elemetn and percolate it down;
let sortedArr = [];
for (let i = arr.length; i > 0; i--) {
sortedArr.push(arr[0]);
arr[0] = arr[arr.length - 1];
arr.pop();
let hole = 0;
while (hole * 2 + 1 < arr.length) {
let minIndex = hole * 2 + 1;
if (minIndex + 1 < arr.length && arr[minIndex + 1] < arr[minIndex])
minIndex = hole * 2 + 2;
if (arr[hole] > arr[minIndex]) {
let tmp = arr[hole];
arr[hole] = arr[minIndex];
arr[minIndex] = tmp;
hole = minIndex;
} else {
break;
}
}
}
return sortedArr;
};
export default heapSort;