Skip to content

Instantly share code, notes, and snippets.

@Luke-Rogerson
Created September 29, 2018 14:04
Show Gist options
  • Save Luke-Rogerson/7359f5abd94fe09d86c9e1a1c915a519 to your computer and use it in GitHub Desktop.
Save Luke-Rogerson/7359f5abd94fe09d86c9e1a1c915a519 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;
}
}
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