Skip to content

Instantly share code, notes, and snippets.

@renatoargh
Created July 17, 2012 20:30
Show Gist options
  • Select an option

  • Save renatoargh/3131843 to your computer and use it in GitHub Desktop.

Select an option

Save renatoargh/3131843 to your computer and use it in GitHub Desktop.
Bubblesort Implementation in Java Script
function swap(array, i, j){
var aux = array[i];
array[i] = array[j];
array[j] = aux;
}
function bubbleSort(array, ascending){
for(i = 0; i < array.length; i++){
for(j = array.length - 1; j >= i + 1; j--){
if((array[j] < array[j - 1]) === ascending){
swap(array, j, j - 1);
}
}
}
}
var array = [5, 4, 3, 2, 1, 0];
console.log("ANTES: " + array);
bubbleSort(array, true);
console.log("DEPOIS: " + array);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment