Created
November 6, 2022 02:45
-
-
Save SparrowMike/ac9642c00b09a8fe81ce36138442cc8b 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
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