Skip to content

Instantly share code, notes, and snippets.

@therealbnut
Last active June 28, 2016 23:22
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 therealbnut/8a6feb83ef9cdcea4627190ef4f974b5 to your computer and use it in GitHub Desktop.
Save therealbnut/8a6feb83ef9cdcea4627190ef4f974b5 to your computer and use it in GitHub Desktop.
// Using XCode 7.3.1 (it's all I had at the time)
extension MutableCollectionType where Generator.Element: Comparable {
mutating func selectionSortInPlace() {
for currentIndex in self.startIndex ..< self.endIndex {
guard let (minIndex, minElement) = zip((currentIndex ..< self.endIndex), self.suffixFrom(currentIndex))
.minElement({ lhs, rhs in (lhs.1 as! Generator.Element) < (rhs.1 as! Generator.Element) })
else {
continue
}
self[minIndex] = self[currentIndex]
self[currentIndex] = minElement as! Generator.Element // swifty code finds compiler bugs
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment