-
Notifications
You must be signed in to change notification settings - Fork 0
/
w5_d1_quadratic_sorts.js
199 lines (165 loc) · 4.98 KB
/
w5_d1_quadratic_sorts.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* Homework - Quadratic Sorts
*
* Problem 1: Insertion Sort
*
* Prompt: Given an unsorted array of integers, return the array sorted
* using insertion sort.
*
* What are the time and auxilliary space complexity?
*
* Input: input {Array}
* Output: {Array}
*
* Example: [3,9,1,4,7] --> [1,3,4,7,9]
*
*
* Problem 2: Selection Sort
*
* Prompt: Given an unsorted array of integers, return the array
* sorted using selection sort.
*
* What are the time and auxilliary space complexity?
*
* Input: input {Array}
* Output: {Array}
*
* Example: [3,9,1,4,7] --> [1,3,4,7,9]
*
*
* Problem 3: Bubble Sort
*
* Prompt: Given an unsorted array of integers, return the array
* sorted using bubble sort.
*
* What are the time and auxilliary space complexity?
*
* Input: input {Array}
* Output: {Array}
*
* Example: [3,9,1,4,7] --> [1,3,4,7,9]
*/
'use strict';
// Time Complexity:
// Auxiliary Space Complexity:
function insertionSort(input) {
// YOUR WORK HERE
}
// Time Complexity:
// Auxiliary Space Complexity:
function selectionSort(input){
// YOUR WORK HERE
}
// Time Complexity:
// Auxiliary Space Complexity:
function bubbleSort(input){
// YOUR WORK HERE
}
////////////////////////////////////////////////////////////
/////////////// DO NOT TOUCH TEST BELOW!!! ///////////////
////////////////////////////////////////////////////////////
console.log('Insertion Sort Tests');
let testCount = [0, 0];
assert(testCount, 'should sort [3,9,1,4,7]', () => {
let test = insertionSort([3,9,1,4,7]);
return arraysEqual(test, [1,3,4,7,9]);
});
assert(testCount, 'should return empty array for empty input', () => {
let test = insertionSort([]);
return arraysEqual(test, []);
});
assert(testCount, 'should sort single-element input', () => {
let test = insertionSort([10]);
return arraysEqual(test, [10]);
});
assert(testCount, 'should sort moderate-sized input', () => {
let sample = [];
for (let i = 0; i < 1000; i++) {
sample.push(Math.floor(Math.random() * 1000));
}
let test = insertionSort(sample.slice());
return test.length === 1000 && arraysEqual(test, sample.sort((a, b) => a - b));
});
console.log('PASSED: ' + testCount[0] + ' / ' + testCount[1], '\n\n');
console.log('Selection Sort Tests');
testCount = [0, 0];
assert(testCount, 'should sort [3,9,1,4,7]', () => {
let test = selectionSort([3,9,1,4,7]);
return arraysEqual(test, [1,3,4,7,9]);
});
assert(testCount, 'should return empty array for empty input', () => {
let test = selectionSort([]);
return arraysEqual(test, []);
});
assert(testCount, 'should sort single-element input', () => {
let test = selectionSort([10]);
return arraysEqual(test, [10]);
});
assert(testCount, 'should sort moderate-sized input', () => {
let sample = [];
for (let i = 0; i < 1000; i++) {
sample.push(Math.floor(Math.random() * 1000));
}
let test = selectionSort(sample.slice());
return test.length === 1000 && arraysEqual(test, sample.sort((a, b) => a - b));
});
console.log('PASSED: ' + testCount[0] + ' / ' + testCount[1], '\n\n');
console.log('Bubble Sort Tests');
testCount = [0, 0];
assert(testCount, 'should sort [3,9,1,4,7]', () => {
let test = bubbleSort([3,9,1,4,7]);
return arraysEqual(test, [1,3,4,7,9]);
});
assert(testCount, 'should return empty array for empty input', () => {
let test = bubbleSort([]);
return arraysEqual(test, []);
});
assert(testCount, 'should sort single-element input', () => {
let test = bubbleSort([10]);
return arraysEqual(test, [10]);
});
assert(testCount, 'should sort moderate-sized input', () => {
let sample = [];
for (let i = 0; i < 1000; i++) {
sample.push(Math.floor(Math.random() * 1000));
}
let test = bubbleSort(sample.slice());
return test.length === 1000 && arraysEqual(test, sample.sort((a, b) => a - b));
});
console.log('PASSED: ' + testCount[0] + ' / ' + testCount[1], '\n\n');
// compare if two flat arrays are equal
function arraysEqual(arr1, arr2) {
if (arr1.length !== arr2.length) { return false; }
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) { return false; }
}
return true;
}
// custom assert function to handle tests
// input: count {Array} - keeps track out how many tests pass and how many total
// in the form of a two item array i.e., [0, 0]
// input: name {String} - describes the test
// input: test {Function} - performs a set of operations and returns a boolean
// indicating if test passed
// output: {undefined}
function assert(count, name, test){
if (!count || !Array.isArray(count) || count.length !== 2) {
count = [0, '*'];
} else {
count[1]++;
}
let pass = 'false';
let errMsg = null;
try {
if (test()) {
pass = ' true';
count[0]++;
}
} catch(e) {
errMsg = e;
}
console.log(' ' + (count[1] + ') ').slice(0,5) + pass + ' : ' + name);
if (errMsg !== null) {
console.log(' ' + errMsg + '\n');
}
}