Last active
May 9, 2019 05:30
-
-
Save JimRottinger/9688c0619c273f1ffe48959c880a4c32 to your computer and use it in GitHub Desktop.
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
const selectionSort = (nums) => { | |
for (let i = 0; i < nums.length - 1; i++) { | |
let smallest = nums[i] | |
let swapIndex = i | |
for (let j = i + 1; j < nums.length; j++) { | |
if (nums[j] < smallest) { | |
smallest = nums[j]; | |
swapIndex = j | |
} | |
} | |
if (i !== swapIndex) { | |
const tmp = nums[i] | |
nums[i] = smallest | |
nums[swapIndex] = tmp | |
} | |
} | |
return nums | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment