Skip to content

Instantly share code, notes, and snippets.

@Luke-Rogerson
Created September 29, 2018 13:44
Show Gist options
  • Save Luke-Rogerson/4ef94ea6c7eb354dbfe8fd3b421dca7a to your computer and use it in GitHub Desktop.
Save Luke-Rogerson/4ef94ea6c7eb354dbfe8fd3b421dca7a to your computer and use it in GitHub Desktop.
SelectionSort created by Luke_Rogerson - https://repl.it/@Luke_Rogerson/SelectionSort
// 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