Created
May 9, 2020 16:41
-
-
Save iamamit-107/2902d5413f6f3c17319239bcb4f47c1f to your computer and use it in GitHub Desktop.
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(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