Created
January 2, 2017 19:52
-
-
Save terakilobyte/8e866e1a1d7b091d31c1182d4f94d961 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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