Skip to content

Instantly share code, notes, and snippets.

@kdnk
Created November 29, 2016 14:25
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 kdnk/49049933d0ad3d01635c17e589c55d20 to your computer and use it in GitHub Desktop.
Save kdnk/49049933d0ad3d01635c17e589c55d20 to your computer and use it in GitHub Desktop.
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