Skip to content

Commit 6e32808

Browse files
committedMar 24, 2016
Add bubble sort
1 parent 4686bd6 commit 6e32808

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// sample of arrays to sort
2+
var arrayRandom = [9, 2, 5, 6, 4, 3, 7, 10, 1, 8];
3+
var arrayOrdered = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
4+
var arrayReversed = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
5+
6+
// swap function helper
7+
function swap(array, i, j) {
8+
var temp = array[i];
9+
array[i] = array[j];
10+
array[j] = temp;
11+
}
12+
13+
// 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
14+
function bubbleSortBasic(array) {
15+
var countOuter = 0;
16+
var countInner = 0;
17+
var countSwap = 0;
18+
19+
for(var i = 0; i < array.length; i++) {
20+
countOuter++;
21+
for(var j = 1; j < array.length; j++) {
22+
countInner++;
23+
if(array[j - 1] > array[j]) {
24+
countSwap++;
25+
swap(array, j - 1, j);
26+
}
27+
}
28+
}
29+
30+
console.log('outer:', countOuter, 'inner:', countInner, 'swap:', countSwap);
31+
return array;
32+
}
33+
34+
bubbleSortBasic(arrayRandom.slice()); // => outer: 10 inner: 90 swap: 21
35+
bubbleSortBasic(arrayOrdered.slice()); // => outer: 10 inner: 90 swap: 0
36+
bubbleSortBasic(arrayReversed.slice()); // => outer: 10 inner: 90 swap: 45
37+
38+
// correct implementation: this is the usual implementation of the bubble sort algorithm. Some loops execution are avoided if not they are not needed
39+
function bubbleSort(array) {
40+
var countOuter = 0;
41+
var countInner = 0;
42+
var countSwap = 0;
43+
44+
var swapped;
45+
do {
46+
countOuter++;
47+
swapped = false;
48+
for(var i = 0; i < array.length; i++) {
49+
countInner++;
50+
if(array[i] && array[i + 1] && array[i] > array[i + 1]) {
51+
countSwap++;
52+
swap(array, i, i + 1);
53+
swapped = true;
54+
}
55+
}
56+
} while(swapped);
57+
58+
console.log('outer:', countOuter, 'inner:', countInner, 'swap:', countSwap);
59+
return array;
60+
}
61+
62+
bubbleSort(arrayRandom.slice()); // => outer: 9 inner: 90 swap: 21
63+
bubbleSort(arrayOrdered.slice()); // => outer: 1 inner: 10 swap: 0
64+
bubbleSort(arrayReversed.slice()); // => outer: 10 inner: 100 swap: 45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// array to sort
2+
var array = [9, 2, 5, 6, 4, 3, 7, 10, 1, 8];
3+
4+
// swap function helper
5+
function swap(array, i, j) {
6+
var temp = array[i];
7+
array[i] = array[j];
8+
array[j] = temp;
9+
}
10+
11+
// 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
12+
function bubbleSortBasic(array) {
13+
for(var i = 0; i < array.length; i++) {
14+
for(var j = 1; j < array.length; j++) {
15+
if(array[j - 1] > array[j]) {
16+
swap(array, j - 1, j);
17+
}
18+
}
19+
}
20+
return array;
21+
}
22+
23+
console.log(bubbleSortBasic(array.slice())); // => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
24+
25+
// correct implementation: this is the usual implementation of the bubble sort algorithm. Some loops execution are avoided if not they are not needed
26+
function bubbleSort(array) {
27+
var swapped;
28+
do {
29+
swapped = false;
30+
for(var i = 0; i < array.length; i++) {
31+
if(array[i] && array[i + 1] && array[i] > array[i + 1]) {
32+
swap(array, i, i + 1);
33+
swapped = true;
34+
}
35+
}
36+
} while(swapped);
37+
return array;
38+
}
39+
40+
console.log(bubbleSort(array.slice())); // => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

0 commit comments

Comments
 (0)