-
Notifications
You must be signed in to change notification settings - Fork 497
/
Copy pathbubble-sort.es6.js
33 lines (29 loc) · 1.12 KB
/
bubble-sort.es6.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
// array to sort
const array = [9, 2, 5, 6, 4, 3, 7, 10, 1, 8];
// be careful: this is a very basic implementation which is nice to understand the deep principle of bubble sort (going through all comparisons) but it can be greatly improved for performances
function bubbleSortBasic(array) {
for(let i = 0; i < array.length; i++) {
for(let j = 1; j < array.length; j++) {
if(array[j - 1] > array[j]) {
[array[j - 1], array[j]] = [array[j], array[j - 1]];
}
}
}
return array;
}
console.log(bubbleSortBasic(array.slice())); // => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
// correct implementation: this is the usual implementation of the bubble sort algorithm. Some loops execution are avoided if not they are not needed
function bubbleSort(array) {
let swapped;
do {
swapped = false;
for(let i = 0; i < array.length; i++) {
if(array[i] && array[i + 1] && array[i] > array[i + 1]) {
[array[i], array[i + 1]] = [array[i + 1], array[i]];
swapped = true;
}
}
} while(swapped);
return array;
}
console.log(bubbleSort(array.slice())); // => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]