Skip to content

Instantly share code, notes, and snippets.

@BetterProgramming
Created May 12, 2019 16:32
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 BetterProgramming/1c6503aa76811b46ca6984b1dc40debe to your computer and use it in GitHub Desktop.
Save BetterProgramming/1c6503aa76811b46ca6984b1dc40debe to your computer and use it in GitHub Desktop.
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