-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap-sort.js
35 lines (31 loc) · 833 Bytes
/
heap-sort.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
function heapify(array, n, i){
let largest = i;
let l = 2 * i + 1;
let r = 2 * i + 2;
if(l < n && array[i] < array[l]){
largest = l;
}
if(r < n && array[largest] < array[r]){
largest = r;
}
if(largest != i){
array[i] = array[i] + array[largest];
array[largest] = array[i] - array[largest];
array[i] = array[i] - array[largest];
heapify(array, n, largest);
}
}
function sort(array){
for(let i = Math.floor(array.length / 2); i > -1; i --){
heapify(array, array.length, i);
}
for(let j = array.length - 1; j > 0; j --){
array[j] = array[j] + array[0];
array[0] = array[j] - array[0];
array[j] = array[j] - array[0];
heapify(array, j, 0);
}
}
let a = [5, 6, 2, 9, 1];
sort(a);
console.log(a);