Skip to content

Instantly share code, notes, and snippets.

@mrlynn
Created February 20, 2019 17:41
Show Gist options
  • Save mrlynn/26350737f17a9b512ce1ad7c4937749f to your computer and use it in GitHub Desktop.
Save mrlynn/26350737f17a9b512ce1ad7c4937749f to your computer and use it in GitHub Desktop.
var countOuter = 0;
var countInner = 0;
var countSwap = 0;
array = [3,9,4,5,1,200,2,88]
dump(array);
var swapped;
do {
countOuter++;
swapped = false;
for(var i = 0; i < array.length; i++) {
countInner++;
if(array[i] && array[i + 1] && array[i] > array[i + 1]) {
countSwap++;
swap(array, i, i + 1);
swapped = true;
}
}
} while(swapped);
console.log('outer:', countOuter, 'inner:', countInner, 'swap:', countSwap);
dump(array);
function swap(array, i, j) {
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
function dump(array) {
for ( var i = 0; i < array.length; i++) {
console.log(i + ": " + array[i] + " ");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment