diff --git a/Assignments/Sorting/SelectionSort.playground/Pages/Generic.xcplaygroundpage/Contents.swift b/Assignments/Sorting/SelectionSort.playground/Pages/Generic.xcplaygroundpage/Contents.swift index b80bb94..8736ceb 100644 --- a/Assignments/Sorting/SelectionSort.playground/Pages/Generic.xcplaygroundpage/Contents.swift +++ b/Assignments/Sorting/SelectionSort.playground/Pages/Generic.xcplaygroundpage/Contents.swift @@ -7,11 +7,32 @@ Turn the function into a generic function so that it can sort any type of array. For practicing purposes you may want to rewrite the function from scratch rather than just copy-pasting. */ - - - - -//assert(selectionSort(["c", "a", "b"]).isSorted()) +func selectionSort(var array: [T]) -> [T] { + guard array.count > 1 else {return array} + + func minIndex(array: [T], startIndex: Int) -> Int { + var minIndex = startIndex + for i in startIndex+1..(var array: [T], @noescape isOrderedBefore: (T, T) -> Bool) -> [T] { + guard array.count > 1 else {return array} + + func minIndex(array: [T], startIndex: Int, @noescape isOrderedBefore: (T, T) -> Bool) -> Int { + var minIndex = startIndex + for i in startIndex+1.. [Int] { - // You may declare array argument with var keyword so that it is copied. +func selectionSort(var array: [Int]) -> [Int] { + guard array.count > 1 else {return array} + + func minIndex(array: [Int], startIndex: Int) -> Int { + var minIndex = startIndex + for i in startIndex+1.. [Int] { assert(selectionSort([1]).isSorted()) assert(selectionSort([1, 2, 3]).isSorted()) -//assert(selectionSort([3, 1, 2]).isSorted()) -//assert(selectionSort([3, 2, 1, 2, 1]).isSorted()) +assert(selectionSort([3, 1, 2]).isSorted()) +assert(selectionSort([3, 2, 1, 2, 1]).isSorted()) /*: [Table of Contents](Table%20of%20Contents) | [Previous](@previous) | [Next](@next) diff --git a/Assignments/Sorting/SelectionSort.playground/Pages/SequenceType Extension.xcplaygroundpage/Contents.swift b/Assignments/Sorting/SelectionSort.playground/Pages/SequenceType Extension.xcplaygroundpage/Contents.swift index 816c349..8698f3f 100644 --- a/Assignments/Sorting/SelectionSort.playground/Pages/SequenceType Extension.xcplaygroundpage/Contents.swift +++ b/Assignments/Sorting/SelectionSort.playground/Pages/SequenceType Extension.xcplaygroundpage/Contents.swift @@ -6,11 +6,31 @@ Instead of only sorting arrays, modify your function to sort any type conforming */ extension SequenceType { - + func selectionSort(@noescape isOrderedBefore: (Generator.Element, Generator.Element) -> Bool) -> [Generator.Element] { + var array = Array(self) + guard array.count > 1 else {return array} + + func minIndex(array: [Generator.Element], startIndex: Int, @noescape isOrderedBefore: (Generator.Element, Generator.Element) -> Bool) -> Int { + var minIndex = startIndex + for i in startIndex+1.. $1.0}).isSorted({$0.0 > $1.0})) +assert([1, 3, 2].selectionSort(<).isSorted()) +assert([1: "b", 2: "a"].selectionSort({$0.0 > $1.0}).isSorted({$0.0 > $1.0})) /*: [Table of Contents](Table%20of%20Contents) | [Previous](@previous) | [Next](@next)