Skip to content

Instantly share code, notes, and snippets.

@mustafadalga
Created October 23, 2021 19:19
Show Gist options
  • Save mustafadalga/a5b20a6f7ee22548b986f66e0dba3c43 to your computer and use it in GitHub Desktop.
Save mustafadalga/a5b20a6f7ee22548b986f66e0dba3c43 to your computer and use it in GitHub Desktop.
Selection Sort Algorithm
function selectionSort(array) {
for (let i = 0; i < array.length; i++) {
let lowestIndex = i
for (let j = i + 1; j < array.length; j++) {
if (array[lowestIndex] > array[j]) {
lowestIndex = j;
}
}
if (i !== lowestIndex) {
let temp = array[lowestIndex];
array[lowestIndex] = array[i];
array[i] = temp;
}
}
return array;
}
const numberList = [...new Set(Array.from({ length: 50 }, () => Math.floor(Math.random() * 250)))];
console.log(selectionSort(numberList))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment