Skip to content

Instantly share code, notes, and snippets.

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