Skip to content

Commit e48780c

Browse files
committed
Add sorting methods test, checking Running Time
1 parent fbb0bb6 commit e48780c

File tree

2 files changed

+157
-89
lines changed

2 files changed

+157
-89
lines changed

src/sort/sorting.test.ts

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import { performance } from "perf_hooks";
2+
3+
import radixSort from "./radixSort";
4+
5+
import mergeSort from "./mergeSort";
6+
import quickSort from "./quickSort";
7+
import heapSort from "./heapSort";
8+
import bubbleSort from "./bubbleSort";
9+
import insertionSort from "./insertionSort";
10+
import selectionSort from "./selectionSort";
11+
12+
interface MethodTime {
13+
method: string;
14+
time: number;
15+
}
16+
17+
describe("Test of Running Time for Sorting Methods", () => {
18+
let t1: number, t2: number;
19+
let method: string;
20+
const nTimes = 10;
21+
const SIZE = 30000;
22+
const times: MethodTime[] = new Array();
23+
24+
beforeEach(() => {
25+
t1 = performance.now();
26+
});
27+
28+
afterEach(() => {
29+
let t2 = performance.now();
30+
const rT = (t2 - t1) / (1000 * nTimes);
31+
// times.set(method, rT);
32+
const mT = { method: method, time: rT };
33+
times.push(mT);
34+
console.log(`${method}: running time ${rT}`);
35+
});
36+
37+
afterAll(() => {
38+
const sortedMethodTimes = times.sort(
39+
(a: MethodTime, b: MethodTime) => a.time - b.time
40+
);
41+
console.log("*** All Methods and Running Times:");
42+
console.log(sortedMethodTimes);
43+
// for (let mt of sortedMethodTimes) {
44+
// console.log(`${mt.method}: ${mt.time}`);
45+
// }
46+
});
47+
48+
test("Radix Sort", () => {
49+
method = "Radix Sort";
50+
for (let i = 0; i < nTimes; i++) {
51+
const arr = Array.from({ length: SIZE }, () =>
52+
Math.floor(Math.random() * 1000)
53+
);
54+
radixSort(arr);
55+
}
56+
});
57+
58+
test("Merge Sort", () => {
59+
method = "Merge Sort";
60+
for (let i = 0; i < nTimes; i++) {
61+
const arr = Array.from({ length: SIZE }, () =>
62+
Math.floor(Math.random() * 1000)
63+
);
64+
mergeSort(arr);
65+
}
66+
});
67+
68+
test("Quick Sort (Random Pivot)", () => {
69+
method = "Quick Sort - Random Pivot";
70+
for (let i = 0; i < nTimes; i++) {
71+
const arr = Array.from({ length: SIZE }, () =>
72+
Math.floor(Math.random() * 1000)
73+
);
74+
quickSort(arr);
75+
}
76+
});
77+
78+
test("Quick Sort (First Element Pivot)", () => {
79+
method = "Quick Sort - First Element Pivot";
80+
for (let i = 0; i < nTimes; i++) {
81+
const arr = Array.from({ length: SIZE }, () =>
82+
Math.floor(Math.random() * 1000)
83+
);
84+
quickSort(arr, 0, arr.length, 0);
85+
}
86+
});
87+
88+
test("Quick Sort (Last Element Pivot)", () => {
89+
method = "Quick Sort - Last Element Pivot";
90+
for (let i = 0; i < nTimes; i++) {
91+
const arr = Array.from({ length: SIZE }, () =>
92+
Math.floor(Math.random() * 1000)
93+
);
94+
quickSort(arr, 0, arr.length, 1);
95+
}
96+
});
97+
98+
test("Quick Sort (Median of 3)", () => {
99+
method = "Quick Sort - Median of 3";
100+
for (let i = 0; i < nTimes; i++) {
101+
const arr = Array.from({ length: SIZE }, () =>
102+
Math.floor(Math.random() * 1000)
103+
);
104+
quickSort(arr, 0, arr.length, 2);
105+
}
106+
});
107+
108+
test("Quick Sort (Median of 3 Random)", () => {
109+
method = "Quick Sort - Median of 3 Random";
110+
for (let i = 0; i < nTimes; i++) {
111+
const arr = Array.from({ length: SIZE }, () =>
112+
Math.floor(Math.random() * 1000)
113+
);
114+
quickSort(arr, 0, arr.length, 4);
115+
}
116+
});
117+
118+
test("Heap Sort", () => {
119+
method = "Heap Sort";
120+
for (let i = 0; i < nTimes; i++) {
121+
const arr = Array.from({ length: SIZE }, () =>
122+
Math.floor(Math.random() * 1000)
123+
);
124+
heapSort(arr);
125+
}
126+
});
127+
128+
test("Bubble Sort", () => {
129+
method = "Bubble Sort";
130+
for (let i = 0; i < nTimes; i++) {
131+
const arr = Array.from({ length: SIZE }, () =>
132+
Math.floor(Math.random() * 1000)
133+
);
134+
bubbleSort(arr);
135+
}
136+
});
137+
138+
test("Insertion Sort", () => {
139+
method = "Insertion Sort";
140+
for (let i = 0; i < nTimes; i++) {
141+
const arr = Array.from({ length: SIZE }, () =>
142+
Math.floor(Math.random() * 1000)
143+
);
144+
insertionSort(arr);
145+
}
146+
});
147+
148+
test("Selection Sort", () => {
149+
method = "Selection Sort";
150+
for (let i = 0; i < nTimes; i++) {
151+
const arr = Array.from({ length: SIZE }, () =>
152+
Math.floor(Math.random() * 1000)
153+
);
154+
selectionSort(arr);
155+
}
156+
});
157+
});

src/sort/testSorting.ts

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)