Skip to content

Instantly share code, notes, and snippets.

@terakilobyte
Created January 2, 2017 19:52
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 terakilobyte/8e866e1a1d7b091d31c1182d4f94d961 to your computer and use it in GitHub Desktop.
Save terakilobyte/8e866e1a1d7b091d31c1182d4f94d961 to your computer and use it in GitHub Desktop.
function bubbleSort(arr) {
let totalOperations = 0;
for (let i = 0; i < arr.length - 1; i++) {
totalOperations++;
for (let j = 0; j < arr.length - 1 - i; j++) {
totalOperations++;
if (arr[j] > arr[j + 1]) {
let tmp = arr[j + 1];
totalOperations++;
arr[j + 1] = arr[j];
totalOperations++;
arr[j] = tmp;
totalOperations++;
}
}
}
console.log(arr, totalOperations);
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment