-
Notifications
You must be signed in to change notification settings - Fork 0
/
heapSort.js
93 lines (81 loc) · 2.58 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
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
//TO:DO fix the colors in heapify
async function heapSort(arr, n) {
// Build heap (rearrange array)
for (var i = n / 2 - 1; i >= 0; i--) {
if(hasPressedStop == true){
return;
}
await heapify(arr, n, i);
}
// One by one extract an element from heap
for (var i = n - 1; i > 0; i--) {
if(hasPressedStop == true){
return;
}
// Move current root to end
var temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
arr[0].style.background = 'cyan';
arr[i].style.background = 'green';
swap(arr[0], arr[i]);
await delayTime(delay);
// call max heapify on the reduced heap
await heapify(arr, i, 0);
}
}
// To heapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
async function heapify(arr, n, i) {
if(hasPressedStop == true){
return;
}
var largest = i; // Initialize largest as root
var l = 2 * i + 1; // left = 2*i + 1
var r = 2 * i + 2; // right = 2*i + 2
// If left child is larger than root
if (l < n && parseInt(arr[l].style.height) > parseInt(arr[largest].style.height)) {
// arr[l].style.background = 'lightblue';
// arr[largest].style.background = 'cyan';
largest = l;
swap(arr[largest], arr[l]);
// arr[l].style.background = '#e43f5a';
}
// If right child is larger than largest so far
if (r < n && parseInt(arr[r].style.height) > parseInt(arr[largest].style.height)) {
// arr[r].style.background = 'lightgreen';
// arr[largest].style.background = 'cyan';
largest = r;
swap(arr[largest], arr[r]);
// arr[l].style.background = '#e43f5a';
}
// If largest is not root
if (largest != i) {
var temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
swap(arr[i], arr[largest]);
// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
}
const heapSortbtn = document.querySelector(".heapSort");
heapSortbtn.addEventListener("click", async function () {
let arr = document.querySelectorAll('.bar');
let n = arr.length;
hasPressedStop = false;
disableSortingBtn();
disableSizeSlider();
disableNewArrayBtn();
enableStopSortingBtn();
await heapSort(arr, n);
arr[0].style.background = 'green';
if (hasPressedStop == true) {
disableSpeedSlider();
} else {
enableSortingBtn();
enableSizeSlider();
}
enableNewArrayBtn();
disableStopSortingBtn();
});