Created
September 29, 2018 13:44
-
-
Save Luke-Rogerson/4ef94ea6c7eb354dbfe8fd3b421dca7a to your computer and use it in GitHub Desktop.
SelectionSort created by Luke_Rogerson - https://repl.it/@Luke_Rogerson/SelectionSort
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
// A simple selection sort algorithm | |
function selectionSort (arr) { | |
let minIndex = 0; | |
let temp = 0; | |
for (let i = 0; i < arr.length; i++) { | |
minIndex = i; | |
for (let j = i + 1; j < arr.length; j++) { | |
if (arr[j] < arr[minIndex]) { | |
minIndex = j; | |
} | |
if (minIndex === j) { | |
temp = arr[minIndex] | |
arr[minIndex] = arr[i]; | |
arr[i] = temp; | |
} | |
} | |
} | |
return arr; | |
} | |
selectionSort ([3,1,12,8,6,23,3,3,100,54,765]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment