Skip to content

Instantly share code, notes, and snippets.

@idealwebsolutions
Last active August 2, 2018 22:42
Show Gist options
  • Save idealwebsolutions/6acb70f189e55fcf4d34387c685dc471 to your computer and use it in GitHub Desktop.
Save idealwebsolutions/6acb70f189e55fcf4d34387c685dc471 to your computer and use it in GitHub Desktop.
(Old) Naive bubblesort
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