Created
August 12, 2024 04:45
-
-
Save kshirish/f414c0b19a8a07fdef5ee38a1592d985 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 - 1; i += 1) { // how many times | |
let min_index = i; | |
for(let j = i + 1; j < arr.length; j += 1) { // starts from where | |
if(arr[j] < arr[min_index]) { | |
min_index = j; | |
} | |
} | |
[arr[min_index], arr[i]] = [arr[i], arr[min_index]]; | |
} | |
return arr; | |
} | |
console.log(selectionSort([2, 8, 10, 4, 3, 6, 7])); | |
console.log(selectionSort([7, 3, 9, 12, 11])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment