Skip to content

Instantly share code, notes, and snippets.

@federicobucchi
Created June 13, 2014 17:49
Show Gist options
  • Save federicobucchi/f37e89bb3ed8a2f0671c to your computer and use it in GitHub Desktop.
Save federicobucchi/f37e89bb3ed8a2f0671c to your computer and use it in GitHub Desktop.
Bubble Sorting
var array = [4, 1, 8, 9, 2, 5, 7, 0, 6, 3];
function bubbleSort( a ) {
var swapped;
var support;
var i;
var n = a.length - 1;
do {
swapped = false;
for ( i = 0; i < n; i++ ) {
if ( a[ i ] > a[ i + 1 ] ) {
support = a[ i ];
a[ i ] = a[ i + 1 ];
a[ i + 1 ] = support;
swapped = true;
}
}
n--;
} while ( swapped );
}
bubbleSort( array );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment