-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharraySort.js
314 lines (270 loc) · 7.33 KB
/
arraySort.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
function makeOrderedArray (n = 0) {
const orderedArray = []
for (let i = 1; i <= n; i += 1) {
orderedArray.push(i)
}
return orderedArray
// Silly way
// return new Array(n).fill().map((value, index) => index + 1);
}
function makeRandomArray (n = 0) {
const randomArray = []
for (let i = 0; i < n; i += 1) {
randomArray.push(Math.floor(Math.random() * n) + 1)
}
return randomArray
// Max length: 16 digits
// return String(Math.random()).slice(2, 2 + n).split('').map(Number);
}
// Mutates array for in place swap
function swapValues (array = [], index1 = 0, index2 = 0) {
const value1 = array[index1]
const value2 = array[index2]
if (index1 === index2) {
console.log(index1 + ': ' + value1 + ' stays in place')
} else {
array[index1] = value2
array[index2] = value1
console.log(index1 + ': ' + value1 + ' swaps with ' + index2 + ': ' + value2)
}
}
// Fisher-Yates shuffle
// For each item of array, swap with one of the remaining items
function shuffleArray (array = []) {
console.log(array)
for (const index of array.keys()) {
const swapIndex = index + Math.floor(Math.random() * (array.length - index))
swapValues(array, index, swapIndex)
}
return array
}
// Raw bubble sort double loop
function bubbleSort1 (array = []) {
console.log(array)
for (let pass = 0; pass < array.length; pass += 1) {
for (let i = 0; i < array.length - 1; i += 1) {
if (array[i] > array[i + 1]) {
swapValues(array, i, i + 1)
}
}
}
return array
}
// Bubble sort with optimizations
function bubbleSort2 (array = []) {
console.log(array)
let isSorted = false
let lastUnsorted = array.length - 1
while (!isSorted) {
isSorted = true
for (let i = 0; i < lastUnsorted; i += 1) {
if (array[i] > array[i + 1]) {
swapValues(array, i, i + 1)
isSorted = false
}
}
lastUnsorted -= 1
}
return array
}
// Find minimum item and swap to beginning
function selectionSort1 (array = []) {
console.log(array)
for (let i = 0; i < array.length - 1; i += 1) {
let minIndex = i
for (let j = i + 1; j < array.length; j += 1) {
if (array[j] < array[minIndex]) {
minIndex = j
}
}
swapValues(array, i, minIndex)
}
return array
}
// Find maximum item and swap to beginning
function selectionSort2 (array = []) {
console.log(array)
for (let i = 0; i < array.length - 1; i += 1) {
let maxIndex = i
for (let j = i + 1; j < array.length; j += 1) {
if (array[j] > array[maxIndex]) {
maxIndex = j
}
}
swapValues(array, i, maxIndex)
}
return array
}
// Find minimum item and swap to end
function selectionSort3 (array = []) {
console.log(array)
for (let i = 0; i < array.length - 1; i += 1) {
let minIndex = 0
for (let j = 0; j < array.length - i; j += 1) {
if (array[j] < array[minIndex]) {
minIndex = j
}
}
swapValues(array, array.length - i - 1, minIndex)
}
return array
}
// Find maximum item and swap to end
function selectionSort4 (array = []) {
console.log(array)
for (let i = 0; i < array.length - 1; i += 1) {
let maxIndex = 0
for (let j = 0; j < array.length - i; j += 1) {
if (array[j] > array[maxIndex]) {
maxIndex = j
}
}
swapValues(array, array.length - i - 1, maxIndex)
}
return array
}
// Swap each item into place on left side of array
function insertionSort1 (array = []) {
console.log(array)
// For each item
for (let i = 1; i < array.length; i += 1) {
// Swap it backwards
for (let j = i; j >= 1; j -= 1) {
if (array[j] < array[j - 1]) {
swapValues(array, j, j - 1)
} else {
break
}
}
}
return array
}
// Shift each item into place on left side of array
function insertionSort2 (array = []) {
console.log(array)
// For each item
for (let i = 1; i < array.length; i += 1) {
const iValue = array[i]
// Shift elements forwards
let j
for (j = i; j >= 1; j -= 1) {
if (iValue < array[j - 1]) {
array[j] = array[j - 1]
} else {
break
}
}
// Insert into sorted place once
// Even if it's same as starting place
array[j] = iValue
console.log('Insert ' + iValue + ' at ' + j)
}
return array
}
// QuickSort optimized for simplicity
// Not optimized for time and space
function quickSort (array = []) {
// Base case: array of length 1 or 0
if (array.length < 2) {
return array
}
// Recursive case: subarray to sort
// Choose last item as the pivot
const pivotIndex = array.length - 1
const pivotValue = array[pivotIndex]
console.log('Pivot is ' + pivotValue + ' at ' + pivotIndex)
// Take advantage of filter to sort around pivot
const lowArray = array.filter(item => item < pivotValue)
const pivotArray = array.filter(item => item === pivotValue)
const highArray = array.filter(item => item > pivotValue)
// Concatenate sorted subarrays
return [...quickSort(lowArray), ...pivotArray, ...quickSort(highArray)]
}
function linearSearch (array = [], target = -1) {
// Built in method
// return array.indexOf(target);
let result = -1
for (const index of array.keys()) {
if (array[index] === target) {
result = index
break
}
}
return result
}
function binarySearch1 (array = [], target = -1) {
let low = 0
let high = array.length - 1
let guess
while (true) {
guess = Math.floor((low + high) / 2)
console.log('guess', guess)
// Base case: found it
if (array[guess] === target) {
return guess
}
// Base case: didn't find it
if (low >= high) {
return -1
}
// Recursive case: too low
if (array[guess] < target) {
low = guess + 1
continue
}
// Recursive case: too high
if (array[guess] > target) {
high = guess - 1
continue
}
}
}
function binarySearch2 (array = [], target = -1) {
const low = 0
const high = array.length - 1
let guess
return (function inner (low, high) {
guess = Math.floor((low + high) / 2)
console.log('guess', guess)
// Base case: found it
if (array[guess] === target) {
return guess
}
// Base case: didn't find it
if (low >= high) {
return -1
}
// Recursive case: too low
if (array[guess] < target) {
low = guess + 1
return inner(low, high)
}
// Recursive case: too high
if (array[guess] > target) {
high = guess - 1
return inner(low, high)
}
}(low, high))
}
console.clear()
const orderedArray = makeOrderedArray(100)
console.log(orderedArray)
const randomArray = makeRandomArray(100)
console.log(randomArray)
// const shuffledArray = shuffleArray(orderedArray)
// console.log(shuffledArray)
// console.log(bubbleSort1(randomArray))
// console.log(bubbleSort2(randomArray))
// console.log(selectionSort1(randomArray))
// console.log(selectionSort2(randomArray))
// console.log(selectionSort3(randomArray))
// console.log(selectionSort4(randomArray))
// console.log(insertionSort1(randomArray))
// console.log(insertionSort2(randomArray))
console.log(quickSort(randomArray))
// console.log(linearSearch(orderedArray, 7))
// console.log(linearSearch(randomArray, 7))
// console.log(binarySearch1(orderedArray, 7))
// console.log(binarySearch1(orderedArray, 77))
// console.log(binarySearch2(orderedArray, 7))
// console.log(binarySearch2(orderedArray, 77))