Last active
January 25, 2022 02:09
-
-
Save kevinjie/ef2ddd2e7fce82d6722c011ba3935d78 to your computer and use it in GitHub Desktop.
selectionSort
This file contains hidden or 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
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