Skip to content

Instantly share code, notes, and snippets.

@iamamit-107
Created May 9, 2020 16:41
Show Gist options
  • Save iamamit-107/2902d5413f6f3c17319239bcb4f47c1f to your computer and use it in GitHub Desktop.
Save iamamit-107/2902d5413f6f3c17319239bcb4f47c1f to your computer and use it in GitHub Desktop.
function selectionSort(arr) {
for (let i = 0; i < arr.length; i++) {
let minIndex = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
if (i !== minIndex) {
const lesser = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = lesser;
}
}
return arr;
}
console.log(selectionSort([10, 4, 3, 8, -10]));
//output : [ 3, 4, 8, 10 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment