forked from JCarlosR/Sorting-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.js
More file actions
37 lines (33 loc) · 885 Bytes
/
HeapSort.js
File metadata and controls
37 lines (33 loc) · 885 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
28
29
30
31
32
33
34
35
36
37
const maxHeap = (input, i) => {
const left = 2 * i + 1
const right = 2 * i + 2
let max = i
if (left < arrLength && input[left] > input[max])
max = left
if (right < arrLength && input[right] > input[max])
max = right
if (max != i) {
swap(input, i, max)
maxHeap(input, max)
}
}
const swap = (input, indexA, indexB) => {
const temp = input[indexA]
input[indexA] = input[indexB]
input[indexB] = temp
}
const heapSort = (input = []) => {
arrLength = input.length
for (let i = Math.floor(arrLength / 2); i >= 0; i -= 1)
maxHeap(input, i)
for (i = input.length - 1; i > 0; i--) {
swap(input, 0, i)
arrLength--
maxHeap(input, 0)
}
return input
}
let arrLength;
//Example Usage:
// let myArray = [2, 4, 1, 6, -7, 8, 5, 9, 3, 4];
// console.log(heapSort(myArray));