Created
September 29, 2018 14:04
-
-
Save Luke-Rogerson/7359f5abd94fe09d86c9e1a1c915a519 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; | |
} | |
} | |
temp = arr[minIndex]; | |
arr[minIndex] = arr[i]; | |
arr[i] = temp; | |
} | |
return arr; | |
} | |
selectionSort([16, 11, 2, 12, 8, 4, 4, 2, 16, 19]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment