Skip to content

Instantly share code, notes, and snippets.

@kdnk
Created November 28, 2016 14:37
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/048cdc987d50060fa79e028489c4dbb2 to your computer and use it in GitHub Desktop.
Save kdnk/048cdc987d50060fa79e028489c4dbb2 to your computer and use it in GitHub Desktop.
main()
function main () {
let arr = [5, 8, 4, 2, 6, 1, 3, 9]
printArray(arr)
straightSelectionSort(arr, arr.length)
printArray(arr)
}
function straightSelectionSort (a, n) {
for (let i = 0; i < n - 1; i++) {
let minIndex = i
for (let j = i + 1; j < n; j++) {
if (a[minIndex] > a[j]) {
minIndex = j
}
}
swap(a, i, minIndex)
}
}
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