-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSort.js
More file actions
65 lines (51 loc) · 1.16 KB
/
bubbleSort.js
File metadata and controls
65 lines (51 loc) · 1.16 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
// bubbleSort.js
function bubbleSort(array) {
//loop through the array untill no changes are made
let changed = false;
let i = 0;
while (true) {
console.log(array)
//swap with next if next is smaller
if (array[i] > array[i+1]) {
temp = array[i];
array[i] = array[i+1];
array[i+1] = temp;
changed = true;
}
//end case: when i is greater than array.length
i = i + 1;
if (i >= array.length) {
if (changed === false) {return array}
else {
changed = false;
i = 0;
} //restart array
}
}
}
function selectionSort(array) {
for (let i = 0; i < array.length; i++) {
console.log(array)
//look for smallest in remaining list
let min = array[i]
let swapperIndex = -1;
for (let j = i + 1; j < array.length; j++) {
if (array[j] < min) {
min = array[j]
swapperIndex = j;
}
}
if (swapperIndex > 0) {
//swap with array[i]
let temp = array[i];
array[i] = min;
array[swapperIndex] = temp;
}
}
return array;
}
// test array:
const test = [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
// const test = [4,3,2,1]
res = selectionSort(test)
console.log(res)