Skip to content

Commit fec3880

Browse files
committed
testing max heap
1 parent f7dae6f commit fec3880

File tree

2 files changed

+103
-32
lines changed

2 files changed

+103
-32
lines changed

_heap.js

Lines changed: 83 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,88 @@
1-
class MinHeap {
2-
constructor() {
3-
this.heap = [];
1+
// // Original Array: [4, 10, 3, 5, 1]
2+
// // 4
3+
// // / \
4+
// // 10 3
5+
// // / \
6+
// // 5 1
7+
8+
// // running [10, 4, 3, 5, 1]
9+
// // M r
10+
// // 10
11+
// // / \
12+
// // 4 3
13+
// // / \
14+
// // 5 1
15+
16+
// // Max Heap:
17+
// // 10
18+
// // / \
19+
// // 5 4
20+
// // / \
21+
// // 3 1
22+
23+
function maxHeapify(arr, n, i) {
24+
let largest = i;
25+
const left = 2 * i + 1;
26+
const right = 2 * i + 2;
27+
28+
if (left < n && arr[left] > arr[largest]) {
29+
largest = left;
30+
}
31+
32+
if (right < n && arr[right] > arr[largest]) {
33+
largest = right;
34+
}
35+
36+
if (largest !== i) {
37+
// Swap arr[i] and arr[largest]
38+
[arr[i], arr[largest]] = [arr[largest], arr[i]];
39+
40+
// Recursively heapify the affected sub-tree
41+
maxHeapify(arr, n, largest);
442
}
43+
}
544

6-
insert(num) {
7-
this.heap.push(num);
8-
this.heapifyUp();
9-
// }does (3.) mean sorting an exist collection either descending or ascending? what does 'collection' means here? please give a simplest example for (3.)
45+
function buildMaxHeap(arr) {
46+
const n = arr.length;
1047

11-
heapifyUp() {
12-
let index = this.heap.length - 1;
13-
while (index > 0) {
14-
let parentIndex = Math.floor((index - 1) / 2);
15-
}
48+
// Build a max heap
49+
// Math.floor(n / 2) - 1:
50+
// start the heapify process from the last non-leaf node in the array.
51+
// This speeds up the algorithm because leaf nodes,
52+
// which make up the bottom half of the heap,
53+
// do not need to be heapified since they have no children.
54+
for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
55+
maxHeapify(arr, n, i);
1656
}
1757
}
58+
59+
function heapSort(arr) {
60+
const n = arr.length;
61+
62+
// Build a max heap
63+
buildMaxHeap(arr);
64+
65+
// Extract elements one by one from the heap
66+
for (let i = n - 1; i > 0; i--) {
67+
// Move current root to the end
68+
[arr[0], arr[i]] = [arr[i], arr[0]];
69+
70+
// Call maxHeapify on the reduced heap
71+
maxHeapify(arr, i, 0);
72+
}
73+
return arr;
74+
}
75+
76+
// const a = [null, 0, 5, 20, 6, 12, 65, 1, 4, 9, 3, 89, 22, 25, 28, 10];
77+
const a = [0, 5, 20, 6, 12, 65, 1, 4, 9, 3, 89, 22, 25, 28, 10];
78+
79+
console.log(heapSort(a));
80+
heapSort([12, 11, 13, 5, 6, 7]);
81+
// 89
82+
// / \
83+
// 12 65
84+
// / \ / \
85+
// 9 5 25 28
86+
// / \ / \ / \ / \
87+
// 4 6 3 0 22 20 1 10
88+
//

_heapMax.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
// // / \
2121
// // 3 1
2222

23-
function maxHeapify(arr, n, i) {
23+
// original [null, 0, 5, 20, 6, 12, 65, 1, 4, 9, 3, 89, 22, 25, 28, 10];
24+
// L
25+
// i l r
26+
// running [null, 20, 5, 0, 6, 12, 65, 1, 4, 9, 3, 89, 22, 25, 28, 10];
27+
28+
const maxHeapify = (arr, n, i) => {
2429
let largest = i;
2530
const left = 2 * i + 1;
2631
const right = 2 * i + 2;
@@ -34,44 +39,39 @@ function maxHeapify(arr, n, i) {
3439
}
3540

3641
if (largest !== i) {
37-
// Swap arr[i] and arr[largest]
3842
[arr[i], arr[largest]] = [arr[largest], arr[i]];
39-
40-
// Recursively heapify the affected sub-tree
4143
maxHeapify(arr, n, largest);
4244
}
43-
}
45+
};
4446

45-
function buildMaxHeap(arr) {
47+
const buildMaxHeap = (arr) => {
4648
const n = arr.length;
47-
4849
// Build a max heap
50+
// Math.floor(n / 2) - 1:
51+
// start the heapify process from the last non-leaf node in the array.
52+
// This speeds up the algorithm because leaf nodes,
53+
// which make up the bottom half of the heap,
54+
// do not need to be heapified since they have no children.
4955
for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
5056
maxHeapify(arr, n, i);
5157
}
52-
}
58+
};
5359

54-
function heapSort(arr) {
60+
const heapSort = (arr) => {
5561
const n = arr.length;
56-
57-
// Build a max heap
5862
buildMaxHeap(arr);
5963

60-
// Extract elements one by one from the heap
6164
for (let i = n - 1; i > 0; i--) {
62-
// Move current root to the end
6365
[arr[0], arr[i]] = [arr[i], arr[0]];
64-
65-
// Call maxHeapify on the reduced heap
6666
maxHeapify(arr, i, 0);
67-
console.log(n, arr);
6867
}
69-
return arr;
70-
}
7168

72-
const a = [null, 0, 5, 20, 6, 12, 65, 1, 4, 9, 3, 89, 22, 25, 28, 10];
69+
return arr;
70+
};
7371

74-
// console.log(heapSort(a));
72+
// const a = [null, 0, 5, 20, 6, 12, 65, 1, 4, 9, 3, 89, 22, 25, 28, 10];
73+
const a = [0, 5, 20, 6, 12, 65, 1, 4, 9, 3, 89, 22, 25, 28, 10];
74+
console.log(heapSort(a));
7575
heapSort([12, 11, 13, 5, 6, 7]);
7676
// 89
7777
// / \

0 commit comments

Comments
 (0)