Skip to content

Instantly share code, notes, and snippets.

@JimRottinger
Last active May 9, 2019 05:30
Show Gist options
  • Save JimRottinger/9688c0619c273f1ffe48959c880a4c32 to your computer and use it in GitHub Desktop.
Save JimRottinger/9688c0619c273f1ffe48959c880a4c32 to your computer and use it in GitHub Desktop.
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