Skip to content

Instantly share code, notes, and snippets.

@kevinjie
Last active January 25, 2022 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinjie/ef2ddd2e7fce82d6722c011ba3935d78 to your computer and use it in GitHub Desktop.
Save kevinjie/ef2ddd2e7fce82d6722c011ba3935d78 to your computer and use it in GitHub Desktop.
selectionSort
export default function sort(originalArray) {
const arr = [...originalArray]
for(let i = 0; i < arr.length - 1; i += 1) {
let minIndex = i
for(let j = i + 1;j < arr.length; j += 1) {
if (arr[minIndex] > arr[j]) {
minIndex = j
}
}
if (minIndex !== i) {
[arr[minIndex], arr[i]] = [arr[i], arr[minIndex]]
}
}
return arr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment