Created
May 12, 2019 16:32
-
-
Save BetterProgramming/1c6503aa76811b46ca6984b1dc40debe to your computer and use it in GitHub Desktop.
This file contains 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
let smallest = nums[i] | |
let swapIndex = i | |
// Search the remainder of the array for the smallest number | |
for (let j = i + 1; j < nums.length; j++) { | |
if (nums[j] < smallest) { | |
smallest = nums[j]; | |
swapIndex = j | |
} | |
} | |
// Swap if the smallest number isn't at the current index | |
if (i !== swapIndex) { | |
const tmp = nums[i] | |
nums[i] = smallest | |
nums[swapIndex] = tmp | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment