-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting-algorithms-test.js
More file actions
139 lines (129 loc) · 3.89 KB
/
sorting-algorithms-test.js
File metadata and controls
139 lines (129 loc) · 3.89 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict';
import {
insertionSort,
selectionSort,
mergeSort,
heapSort,
quickSort,
bubbleSort,
shellSort,
combSort,
radixSort,
radixSortDecimalDigits,
} from './sorting-algorithms.js'
function runTests() {
function compareNumbers(a, b) {
return a - b;
}
function randArr(len) {
let arr = [];
for (let i = 0; i < len; i++) {
let randVal = Math.floor(Math.random() * 20000 - 10000);
arr.push(randVal);
}
return arr;
}
const sortFunctions = [
insertionSort,
selectionSort,
mergeSort,
heapSort,
quickSort,
bubbleSort,
shellSort,
combSort,
radixSort,
radixSortDecimalDigits,
];
sortFunctions.forEach(function(sort) {
let arr10Items = randArr(10);
let arr10ItemsSorted = [...arr10Items].sort(compareNumbers);
let arr100Items = randArr(100);
let arr100ItemsSorted = [...arr100Items].sort(compareNumbers);
let arr1000Items = randArr(1000);
let arr1000ItemsSorted = [...arr1000Items].sort(compareNumbers);
let arr10000Items = randArr(10000);
let arr10000ItemsSorted = [...arr10000Items].sort(compareNumbers);
let cases = [
{
name: 'three elements',
arr: [3, 1, 2],
expected: [1, 2, 3],
},
{
name: 'two elements',
arr: [2, 1],
expected: [1, 2],
},
{
name: 'empty array',
arr: [],
expected: [],
},
{
name: 'one element',
arr: [1],
expected: [1],
},
{
name: 'already sorted',
arr: [1, 2, 3],
expected: [1, 2, 3],
},
{
name: 'four elements',
arr: [5, 1, 3, 2],
expected: [1, 2, 3, 5],
},
{
name: 'all sequential',
arr: [5, 4, 1, 3, 0, 2],
expected: [0, 1, 2, 3, 4, 5],
},
{
name: 'with duplicate values',
arr: [6, 6, 3, 3, 3, 1, 3, 2],
expected: [1, 2, 3, 3, 3, 3, 6, 6],
},
{
name: 'with negative numbers',
arr: [14, 7, 19, -3, -6, 0, 12, -9],
expected: [-9, -6, -3, 0, 7, 12, 14, 19],
},
{
name: 'more negatives',
arr: [355, -8632, -7647, 8989, 7727, -9333, -7522, -6265, 4217, -3158],
expected: [-9333, -8632, -7647, -7522, -6265, -3158, 355, 4217, 7727, 8989],
},
{
name: 'random array of 10 items',
arr: arr10Items,
expected: arr10ItemsSorted,
},
{
name: 'random array of 100 items',
arr: arr100Items,
expected: arr100ItemsSorted,
},
{
name: 'random array of 1000 items',
arr: arr1000Items,
expected: arr1000ItemsSorted,
},
{
name: 'random array of 10000 items',
arr: arr10000Items,
expected: arr10000ItemsSorted,
},
]
cases.forEach(function(c) {
console.log(`[${sort.name}] Running test "${c.name}"...`);
let result = sort(c.arr);
if (c.expected.length != result.length || !c.expected.every(function(value, index) { return value === result[index]})) {
console.error(`Got ${JSON.stringify(result)}, want ${JSON.stringify(c.expected)}`);
process.exit(1);
}
});
});
}
runTests();