-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheapsort.js
76 lines (59 loc) · 2.44 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
// Helper methods
function childrenIndices(index) {
// return left and right child index
return [index * 2 + 1, index * 2 + 2];
}
function heapifyDown(minHeap, index) {
[leftChildIndex, rightChildIndex] = childrenIndices(index);
while (minHeap[leftChildIndex] !== undefined) {
var smallestChildIndex = leftChildIndex;
if (minHeap[rightChildIndex] !== undefined) {
smallestChildIndex =
minHeap[leftChildIndex] < minHeap[rightChildIndex]
? leftChildIndex
: rightChildIndex;
}
if (minHeap[smallestChildIndex] > minHeap[index]) {
break;
} else {
[minHeap[smallestChildIndex], minHeap[index]] = [
minHeap[index],
minHeap[smallestChildIndex]
];
index = smallestChildIndex;
[leftChildIndex, rightChildIndex] = childrenIndices(index);
}
}
}
function heapSort(sequence) {
var minHeap = [];
// grab every element in the array and store it in the heap
for (let i = 0; i < sequence.length; i++) {
minHeap.push(sequence[i]);
}
// heapify down all elements in the heap (which are all elements of the array), starting from the bottom of the heap (the last index of the heap)
for (let i = minHeap.length - 1; i >= 0; i--) {
heapifyDown(minHeap, i);
}
var result = [];
// remember [] is truthy! So we use minHeap.length !== 0 as the condition
while (minHeap.length !== 0 && minHeap[0] !== Number.MAX_VALUE) {
var smallest = minHeap[0];
result.push(smallest);
// When we are finished with analyzing the minHeap's first element, replace it with MAX_NUMBER, so that it gets anchored to the bottom, never to be used again
// If this MAX_NUMBER pops up, we know to exit the while loop
// The alternative is to use a pointer to let us know where the end of the heap is, and when to escape from the while loop
minHeap[0] = Number.MAX_VALUE;
heapifyDown(minHeap, 0);
}
return result;
}
// Testing
var sequence = [10, -1, 2, 5, 0, 6, 4, -5];
var ans = heapSort(sequence);
console.log(ans.toString()); // [-5, -1, 0, 2, 4, 5, 6, 10]
// To double check, we will use JS' sort() function to sort the input sequence array
sequence.sort((a, b) => {
return a - b
})
console.log(ans.toString() === sequence.toString())