Skip to content

Commit 01de729

Browse files
authored
Improved BubbleSort and added Doctests (#443)
* BubbleSort | using alternativeBubbleSort() in it's implementation
1 parent e92b5d2 commit 01de729

File tree

1 file changed

+60
-28
lines changed

1 file changed

+60
-28
lines changed

Sorts/BubbleSort.js

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,40 @@
1-
/* Bubble Sort is a algorithm to sort an array. It
2-
* compares adjacent element and swaps thier position
3-
* The big O on bubble sort in worst and best case is O(N^2).
4-
* Not efficient.
1+
/* Bubble Sort is an algorithm to sort an array. It
2+
* compares adjacent element and swaps thier position
3+
* The big O on bubble sort in worst and best case is O(N^2).
4+
* Not efficient.
5+
*
6+
* In bubble sort, we keep iterating while something was swapped in
7+
* the previous inner-loop iteration. By swapped I mean, in the
8+
* inner loop iteration, we check each number if the number proceeding
9+
* it is greater than itself, if so we swap them.
10+
*
11+
* Wikipedia: https://en.wikipedia.org/wiki/Bubble_sort
512
*/
613

14+
/*
15+
* Doctests
16+
*
17+
* > bubbleSort([5, 4, 1, 2, 3])
18+
* [1, 2, 3, 4, 5]
19+
* > bubbleSort([])
20+
* []
21+
* > bubbleSort([1, 2, 3])
22+
* [1, 2, 3]
23+
*
24+
* > alternativeBubbleSort([5, 4, 1, 2, 3])
25+
* [1, 2, 3, 4, 5]
26+
* > alternativeBubbleSort([])
27+
* []
28+
* > alternativeBubbleSort([1, 2, 3])
29+
* [1, 2, 3]
30+
*/
31+
32+
/*
33+
* Using 2 for loops
34+
*/
735
function bubbleSort (items) {
836
const length = items.length
37+
938
for (let i = (length - 1); i > 0; i--) {
1039
// Number of passes
1140
for (let j = (length - i); j > 0; j--) {
@@ -16,31 +45,28 @@ function bubbleSort (items) {
1645
}
1746
}
1847
}
19-
}
20-
21-
// Implementation of bubbleSort
2248

23-
var ar = [5, 6, 7, 8, 1, 2, 12, 14]
24-
// Array before Sort
25-
console.log('-----before sorting-----')
26-
console.log(ar)
27-
bubbleSort(ar)
28-
// Array after sort
29-
console.log('-----after sorting-----')
30-
console.log(ar)
49+
return items
50+
}
3151

32-
/* alternative implementation of bubble sort algorithm.
33-
Using a while loop instead. For educational purposses only
34-
*/
3552
/*
36-
*In bubble sort, we keep iterating while something was swapped in
37-
*the previous inner-loop iteration. By swapped I mean, in the
38-
*inner loop iteration, we check each number if the number proceeding
39-
*it is greater than itself, if so we swap them.
53+
* Implementation of 2 for loops method
4054
*/
55+
var array1 = [5, 6, 7, 8, 1, 2, 12, 14]
56+
// Before Sort
57+
console.log('\n- Before Sort | Implementation using 2 for loops -')
58+
console.log(array1)
59+
// After Sort
60+
console.log('- After Sort | Implementation using 2 for loops -')
61+
console.log(bubbleSort(array1))
62+
console.log('\n')
4163

64+
/*
65+
* Using a while loop and a for loop
66+
*/
4267
function alternativeBubbleSort (arr) {
4368
let swapped = true
69+
4470
while (swapped) {
4571
swapped = false
4672
for (let i = 0; i < arr.length - 1; i++) {
@@ -50,12 +76,18 @@ function alternativeBubbleSort (arr) {
5076
}
5177
}
5278
}
79+
5380
return arr
5481
}
5582

56-
// test
57-
console.log('-----before sorting-----')
58-
var array = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1]
59-
console.log(array)
60-
console.log('-----after sorting-----')
61-
console.log(alternativeBubbleSort(array))
83+
/*
84+
* Implementation of a while loop and a for loop method
85+
*/
86+
var array2 = [5, 6, 7, 8, 1, 2, 12, 14]
87+
// Before Sort
88+
console.log('\n- Before Sort | Implementation using a while loop and a for loop -')
89+
console.log(array2)
90+
// After Sort
91+
console.log('- After Sort | Implementation using a while loop and a for loop -')
92+
console.log(alternativeBubbleSort(array2))
93+
console.log('\n')

0 commit comments

Comments
 (0)