Skip to content

Instantly share code, notes, and snippets.

@SparrowMike
Created November 6, 2022 02:45
Show Gist options
  • Save SparrowMike/ac9642c00b09a8fe81ce36138442cc8b to your computer and use it in GitHub Desktop.
Save SparrowMike/ac9642c00b09a8fe81ce36138442cc8b to your computer and use it in GitHub Desktop.
function selectionSort(arr) {
for(let i = 0; i < arr.length; i++) {
let smallest = i
for(let j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[smallest]) {
smallest = j
}
}
if (smallest !== i) {
[arr[i], arr[smallest]] = [arr[smallest], arr[i]]
}
}
return arr
}
selectionSort([4,61,7,23,15,12,86,31])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment