Skip to content

Instantly share code, notes, and snippets.

@viiiprock
Last active June 28, 2020 22:25
Show Gist options
  • Select an option

  • Save viiiprock/6e3a0e3e993a93cf384b98c2c721cc00 to your computer and use it in GitHub Desktop.

Select an option

Save viiiprock/6e3a0e3e993a93cf384b98c2c721cc00 to your computer and use it in GitHub Desktop.
Algorithms
function selectionSort(arr) {
for (let i = 0; i < arr.length; i++) {
// find the smallest index
let minIndex = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[minIndex] > arr[j]) {
minIndex = j;
}
}
// swap the index
if (minIndex !== i) {
var temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
// use ES6 destruction array to swap
// [arr[i], arr[minIndex]]=[arr[minIndex], arr[i]]
}
}
return arr;
}
const arr = [45, 58, 11, 25, 34, 32, 97, 58];
selectionSort(arr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment