Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 87a4693

Browse files
committed
implemented heap sort
1 parent d647668 commit 87a4693

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

‎src/sorting/heap.js‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// To heapify a subtree rooted with node 'index' which is
2+
// an index in arr[]. 'size' is size of heap
3+
const heapify = (arr, size, index) => {
4+
let largest = index; // Initialize largest as root
5+
const left = 2 * index + 1;
6+
const right = 2 * index + 2;
7+
8+
// If left child is larger than root
9+
if (left < size && arr[left] > arr[largest]) {
10+
largest = left;
11+
}
12+
13+
// If right child is larger than root
14+
if (right < size && arr[right] > arr[largest]) {
15+
largest = right;
16+
}
17+
18+
// If largest is not root
19+
if (largest !== index) {
20+
// swap values
21+
[arr[index], arr[largest]] = [arr[largest], arr[index]];
22+
23+
// Recursively heapify the affected sub-tree
24+
heapify(arr, size, largest);
25+
}
26+
};
27+
28+
const heapSort = (arr) => {
29+
// No need to sort the array if the array only has one element or empty
30+
if (arr.length < 2) return arr;
31+
32+
const result = arr.slice(0);
33+
34+
// Build heap (rearrange array) for first time
35+
for (let i = Math.floor(result.length / 2 - 1); i >= 0; i -= 1) {
36+
heapify(result, result.length, i);
37+
}
38+
39+
// One by one extract an element from heap and again rearrange
40+
for (let i = result.length - 1; i >= 0; i -= 1) {
41+
// Move current root to end
42+
[result[0], result[i]] = [result[i], result[0]];
43+
44+
// call max heapify on the reduced heap
45+
heapify(result, i, 0);
46+
}
47+
48+
return result;
49+
};
50+
51+
module.exports = heapSort;

‎tests/sorting/heap.test.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const heapSort = require('../../src/sorting/heap');
2+
3+
describe('heap sort unit test:', () => {
4+
it('Test#1 simple', async () => {
5+
const want = [2, 3, 5, 6, 7, 9];
6+
const got = heapSort([5, 3, 7, 6, 2, 9]);
7+
expect(got).toEqual(want);
8+
});
9+
10+
it('Test#2 duplicate value', async () => {
11+
const want = [2, 3, 5, 5, 6, 7, 7, 9];
12+
const got = heapSort([5, 3, 7, 6, 2, 9, 7, 5]);
13+
expect(got).toEqual(want);
14+
});
15+
16+
it('Test#3 with negative value', async () => {
17+
const want = [-7, -7, -3, 6, 17, 34, 42, 50, 52, 83, 87, 89, 96];
18+
const got = heapSort([83, 52, 89, 42, 6, 87, 50, 17, 34, 96, -7, -3, -7]);
19+
expect(got).toEqual(want);
20+
});
21+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /