Skip to content

Instantly share code, notes, and snippets.

@dlo
Last active June 28, 2016 17:11
Show Gist options
  • Save dlo/ca2380396a4e163b87522d4c66e5ce5f to your computer and use it in GitHub Desktop.
Save dlo/ca2380396a4e163b87522d4c66e5ce5f to your computer and use it in GitHub Desktop.
func selectionSort(_ array: [Int]) -> [Int] {
guard let minValue = array.min(), let index = array.index(of: minValue) else {
return []
}
let ranges = [0..<index, index.advanced(by: 1)..<array.endIndex]
return [minValue] + selectionSort(ranges.lazy.flatMap { array[$0] })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment