Created
October 23, 2021 19:19
-
-
Save mustafadalga/a5b20a6f7ee22548b986f66e0dba3c43 to your computer and use it in GitHub Desktop.
Selection Sort Algorithm
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(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