Skip to content

Instantly share code, notes, and snippets.

@kshirish
Created August 12, 2024 04:45
Show Gist options
  • Save kshirish/f414c0b19a8a07fdef5ee38a1592d985 to your computer and use it in GitHub Desktop.
Save kshirish/f414c0b19a8a07fdef5ee38a1592d985 to your computer and use it in GitHub Desktop.
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