forked from qkraudghgh/coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheapSort.js
More file actions
27 lines (24 loc) · 846 Bytes
/
heapSort.js
File metadata and controls
27 lines (24 loc) · 846 Bytes
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
const arr = [5, 4, 3, 2, 1];
function heapify(unsorted, index, heapSize) {
let largest = index;
const leftIndex = 2 * index + 1;
const rightIndex = 2 * index + 2;
if (leftIndex < heapSize && unsorted[leftIndex] > unsorted[largest]) largest = leftIndex;
if (rightIndex < heapSize && unsorted[rightIndex] > unsorted[largest]) largest = rightIndex;
if (largest != index) {
[unsorted[largest], unsorted[index]] = [unsorted[index], unsorted[largest]];
heapify(unsorted, largest, heapSize)
}
}
function heapSort(unsorted) {
const length = unsorted.length;
for (let i = Math.floor(length)-1; i > 0; i--) {
heapify(unsorted, i, length);
}
for (let i = length-1; i > 0; i--) {
[unsorted[0], unsorted[i]] = [unsorted[i], unsorted[0]];
heapify(unsorted, 0, i);
}
return unsorted;
}
console.log(heapSort(arr))