Created
November 28, 2016 14:37
-
-
Save kdnk/048cdc987d50060fa79e028489c4dbb2 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] | |
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