-
Notifications
You must be signed in to change notification settings - Fork 0
/
ascending-heapSort.js
62 lines (42 loc) · 1.02 KB
/
ascending-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
function HeapSort(array) {
var n = array.length;
for (let i = Math.floor(n / 2 - 1); i >= 0; i--) {
heapify(array, i, array.length);
}
// console.log(array)
// Move current root to end
for (let i = n - 1; i > 0; i--) {
var temp = array[0];
array[0] = array[i];
array[i] = temp;
heapify(array, 0,i);
}
console.log(array);
}
function heapify(array, index, size) {
let largest = index;
let left = 2 * index + 1;
let right = 2 * index + 2;
// find max from left child, right child, max
if (left < size && array[left] > array[largest]) {
largest = left;
}
if (right < size && array[right] > array[largest]) {
largest = right;
}
if (largest != index) {
swap(array, index,largest);
heapify(array, largest,size);
} else {
// console.log(largest);
return;
}
return;
}
function swap(array, a, b) {
let temp = array[a];
array[a] = array[b];
array[b] = temp;
}
var arr = [5, 12, 11, 13, 4, 6, 7];
HeapSort(arr);