-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathheapSort.js
More file actions
60 lines (45 loc) · 1.64 KB
/
heapSort.js
File metadata and controls
60 lines (45 loc) · 1.64 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
import updateDiv from "../visualization.js";
const divs = document.querySelector("#visualizer-container").children;
function swap(array, i, j, timeout) {
updateDiv(divs[i], "yellow", timeout, array[i]); // color update
updateDiv(divs[j], "yellow", timeout, array[j]); // color update
let temp = array[i];
array[i] = array[j];
array[j] = temp;
updateDiv(divs[i], "yellow", timeout, array[i]); // color update
updateDiv(divs[j], "yellow", timeout, array[j]); // color update
updateDiv(divs[i], "rgb(225, 186, 253)", timeout, array[i]); // color update
updateDiv(divs[j], "rgb(225, 186, 253)", timeout, array[j]); // color update
}
function maxHeapify(array, n, i, timeout) {
let largest = i;
let l = 2 * i + 1;
let r = 2 * i + 2;
if (l < n && array[l] > array[largest]) {
largest = l;
}
if (r < n && array[r] > array[largest]) {
largest = r;
}
if (largest != i) {
swap(array, i, largest, timeout);
maxHeapify(array, n, largest, timeout);
}
}
function heapSort(array, timeout) {
let arrayLength = array.length;
for (let i = Math.floor(arrayLength / 2) - 1; i >= 0; i--) {
maxHeapify(array, arrayLength, i, timeout);
}
let i;
for (i = arrayLength - 1; i > 0; i--) {
swap(array, 0, i, timeout);
updateDiv(divs[i], "green", timeout, array[i]); // color update
updateDiv(divs[i], "yellow", timeout, array[i]); // color update
maxHeapify(array, i, 0, timeout);
updateDiv(divs[i], "red", timeout, array[i]); // color update
updateDiv(divs[i], "green", timeout, array[i]); // color update
}
updateDiv(divs[i], "green", timeout, array[i]); // color update
}
export default heapSort;