Skip to content

Commit 60c4c33

Browse files
committed
Add Heap Sort test
1 parent 574853c commit 60c4c33

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

src/data-structures/heaps/bHNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ class BHNode<T> {
44
constructor(public key: T, public val: number) {}
55
}
66

7-
export = BHNode;
7+
export default BHNode;

src/data-structures/heaps/minHeap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,4 @@ class MinHeap<T> {
343343
};
344344
}
345345

346-
export = MinHeap;
346+
export default MinHeap;

src/sort/heapSort.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import heapSort from "./heapSort";
2+
3+
describe("Heap Sort Test", () => {
4+
test("Testing if HS sort a random array", () => {
5+
const nTimes = 100;
6+
const SIZE = 100;
7+
for (let i = 0; i < nTimes; i++) {
8+
const arr = Array.from({ length: SIZE }, () =>
9+
Math.floor(Math.random() * 1000)
10+
);
11+
const sortedArr = heapSort(arr);
12+
const jsSortedArr = arr.slice().sort((a, b) => a - b);
13+
expect(sortedArr).toEqual(jsSortedArr);
14+
}
15+
});
16+
});

src/sort/heapSort.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
const MinHeap = require("../data-structures/minHeap");
1+
import MinHeap from "../data-structures/heaps/minHeap";
22

33
const heapSort = (arr: number[]) => {
44
const minHeap = new MinHeap();
55
const result = [];
6-
minHeap.buildHeap(arr);
6+
minHeap.buildHeap(arr, [...arr.keys()]);
77
while (!minHeap.isEmpty()) {
8-
result.push(minHeap.dequeue().val);
8+
result.push(minHeap.dequeue()!.val);
99
}
1010
return result;
1111
};
1212

13-
export = heapSort;
13+
export default heapSort;

0 commit comments

Comments
 (0)