Makes sense. Here’s a revision. It’s not as simple, but it does just one
pass through the array:
func selectionSort(_ array: [Int]) -> [Int] {
guard let first = array.first else { return }
let (index, minValue) = array.enumerated().reduce((0, first)) { (carry,
item) in
if item.element < carry.1 { return item } else { return carry }
}
let ranges = [0..<index, index.advanced(by: 1)..<array.endIndex]
return [minValue] + selectionSort(ranges.flatMap { array[$0] })
}
~Dan
···
On Tue, Jun 28, 2016 at 9:58 PM, Erica Sadun <erica@ericasadun.com> wrote:
Most everyone is doing two passes, one to get the minimum value, another
to get its index.
I aesthetically prefer using enumerate to do both at once.-- E