-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
51 lines (43 loc) · 1.07 KB
/
index.js
File metadata and controls
51 lines (43 loc) · 1.07 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
// --- Directions
// Implement bubbleSort, selectionSort, and mergeSort
/**
Name Worst Case Runtime Difficulty
Bubble n^2 easiest
Selection n^2 easier
Merge n*log(n) medium
*/
function bubbleSort(arr) {
const len = arr.length;
for(let i = 0; i < len; i++) {
for (let j = 0; j < len - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
const lesser = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = lesser;
}
}
}
return arr;
}
function selectionSort(arr) {
const len = arr.length;
for (let i = 0; i < len; i++) {
let indexOfMin = i;
for (let j = i + 1; j < len; j++) {
if (arr[j] < arr[indexOfMin]) {
indexOfMin = j;
}
}
if (indexOfMin !== i) {
const lesser = arr[indexOfMin];
arr[indexOfMin] = arr[i];
arr[i] = lesser;
}
}
return arr;
}
function mergeSort(arr) {
}
function merge() {
}
module.exports = { bubbleSort, selectionSort, mergeSort, merge };