Created
November 29, 2016 14:25
-
-
Save kdnk/49049933d0ad3d01635c17e589c55d20 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
main() | |
function main () { | |
// let arr = [5, 8, 4, 2, 6, 1, 3, 9] | |
let arr = [6, 4, 1, 7, 3, 9, 8] | |
printArray(arr) | |
straightInsertionSort(arr, arr.length) | |
printArray(arr) | |
} | |
function straightInsertionSort(a, n) { | |
for (let i = 1; i < n; i++) { | |
const t = a[i] | |
let j = i - 1 | |
while (j >= 0 && a[j] > t) { | |
a[j + 1] = a[j] | |
j-- | |
} | |
a[j + 1] = t | |
} | |
} | |
function swap (a, idx1, idx2) { | |
const t = a[idx1] | |
a[idx1] = a[idx2] | |
a[idx2] = t | |
} | |
function printArray (a) { | |
a.join(' ') | |
console.log(a) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment