Skip to content

Instantly share code, notes, and snippets.

@dvidsilva
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvidsilva/c2541997dcb1e5532fbc to your computer and use it in GitHub Desktop.
Save dvidsilva/c2541997dcb1e5532fbc to your computer and use it in GitHub Desktop.
Bubble sort in JS
var a = [199, 123, 23, 1241, 2412, 123 ,44, 5, 23, 1, 13];
function sortWithBubble(arr)
{
var swapped;
do {
swapped = false;
for (var i=0; i < arr.length-1; i++) {
if (arr[i] > arr[i+1]) {
var temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
swapped = true;
}
}
} while (swapped);
}
sortWithBubble(a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment