Last active
August 2, 2018 22:42
-
-
Save idealwebsolutions/6acb70f189e55fcf4d34387c685dc471 to your computer and use it in GitHub Desktop.
(Old) Naive bubblesort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function bubbleSort(array) { | |
var swapped = true; | |
// Iterate until swaps no longer occur | |
while (swapped) { | |
// Assume swaps no longer needed | |
swapped = false; | |
// Iterate entire array | |
for (var i = 0; i < array.length; i++) { | |
var next = array[i - 1]; | |
var cur = array[i]; | |
// If next element is greater than current, swap | |
if (next > cur) { | |
array[i - 1] = cur; | |
array[i] = next; | |
// Swap occured, keep iterating | |
swapped = true; | |
} | |
} | |
} | |
return array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment