-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheapSort.js
53 lines (48 loc) · 1.49 KB
/
heapSort.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
49
50
51
52
53
function buildMaxHeap(array) {
const firstParentIdx = Math.floor((array.length - 2) / 2);
for (let currentIdx = firstParentIdx; currentIdx >= 0; currentIdx--) {
siftDown(currentIdx, array.length - 1, array);
}
}
function siftDown(currentIdx, endIdx, heap) {
let childOneIdx = currentIdx * 2 + 1;
while (childOneIdx <= endIdx) {
const childTwoIdx = currentIdx * 2 + 2 <= endIdx ? currentIdx * 2 + 2 : -1;
let idxToSwap;
if (childTwoIdx !== -1 && heap[childTwoIdx].style.height > heap[childOneIdx].style.height) {
idxToSwap = childTwoIdx;
} else {
idxToSwap = childOneIdx;
}
if (heap[idxToSwap].style.height > heap[currentIdx].style.height) {
swap(currentIdx, idxToSwap, heap);
currentIdx = idxToSwap;
childOneIdx = currentIdx * 2 + 1;
} else {
return;
}
}
}
function swap(i, j, array) {
const temp = array[j].style.height;
array[j].style.height = array[i].style.height;
array[i].style.height = temp;
}
async function heapSort(array) {
buildMaxHeap(array);
for (let endIdx = array.length - 1; endIdx > 0; endIdx--) {
await delay(sortingSpeed)
swap(0, endIdx, array);
siftDown(0, endIdx - 1, array);
}
return array;
}
heapSortButton.addEventListener('click', () => {
var nodes = Array.prototype.slice.call(document.querySelector(".createColumns").children);
async function repeatChain(times, chain) {
for (let i = 0; i < times; i++) {
await chain(nodes);
}
}
repeatChain(1, heapSort)
})