Skip to content

Improved BubbleSort and added Doctests #443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 60 additions & 28 deletions Sorts/BubbleSort.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
/* Bubble Sort is a algorithm to sort an array. It
* compares adjacent element and swaps thier position
* The big O on bubble sort in worst and best case is O(N^2).
* Not efficient.
/* Bubble Sort is an algorithm to sort an array. It
* compares adjacent element and swaps thier position
* The big O on bubble sort in worst and best case is O(N^2).
* Not efficient.
*
* In bubble sort, we keep iterating while something was swapped in
* the previous inner-loop iteration. By swapped I mean, in the
* inner loop iteration, we check each number if the number proceeding
* it is greater than itself, if so we swap them.
*
* Wikipedia: https://en.wikipedia.org/wiki/Bubble_sort
*/

/*
* Doctests
*
* > bubbleSort([5, 4, 1, 2, 3])
* [1, 2, 3, 4, 5]
* > bubbleSort([])
* []
* > bubbleSort([1, 2, 3])
* [1, 2, 3]
*
* > alternativeBubbleSort([5, 4, 1, 2, 3])
* [1, 2, 3, 4, 5]
* > alternativeBubbleSort([])
* []
* > alternativeBubbleSort([1, 2, 3])
* [1, 2, 3]
*/

/*
* Using 2 for loops
*/
function bubbleSort (items) {
const length = items.length

for (let i = (length - 1); i > 0; i--) {
// Number of passes
for (let j = (length - i); j > 0; j--) {
Expand All @@ -16,31 +45,28 @@ function bubbleSort (items) {
}
}
}
}

// Implementation of bubbleSort

var ar = [5, 6, 7, 8, 1, 2, 12, 14]
// Array before Sort
console.log('-----before sorting-----')
console.log(ar)
bubbleSort(ar)
// Array after sort
console.log('-----after sorting-----')
console.log(ar)
return items
}

/* alternative implementation of bubble sort algorithm.
Using a while loop instead. For educational purposses only
*/
/*
*In bubble sort, we keep iterating while something was swapped in
*the previous inner-loop iteration. By swapped I mean, in the
*inner loop iteration, we check each number if the number proceeding
*it is greater than itself, if so we swap them.
* Implementation of 2 for loops method
*/
var array1 = [5, 6, 7, 8, 1, 2, 12, 14]
// Before Sort
console.log('\n- Before Sort | Implementation using 2 for loops -')
console.log(array1)
// After Sort
console.log('- After Sort | Implementation using 2 for loops -')
console.log(bubbleSort(array1))
console.log('\n')

/*
* Using a while loop and a for loop
*/
function alternativeBubbleSort (arr) {
let swapped = true

while (swapped) {
swapped = false
for (let i = 0; i < arr.length - 1; i++) {
Expand All @@ -50,12 +76,18 @@ function alternativeBubbleSort (arr) {
}
}
}

return arr
}

// test
console.log('-----before sorting-----')
var array = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1]
console.log(array)
console.log('-----after sorting-----')
console.log(alternativeBubbleSort(array))
/*
* Implementation of a while loop and a for loop method
*/
var array2 = [5, 6, 7, 8, 1, 2, 12, 14]
// Before Sort
console.log('\n- Before Sort | Implementation using a while loop and a for loop -')
console.log(array2)
// After Sort
console.log('- After Sort | Implementation using a while loop and a for loop -')
console.log(alternativeBubbleSort(array2))
console.log('\n')