Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 85c2417

Browse files
Add bubble sort in es6
1 parent e42b1b7 commit 85c2417

File tree

2 files changed

+90
-0
lines changed

2 files changed

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /