Skip to content

selectionSort #26

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: assignment001
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<T:Comparable>(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..<array.count where array[i] < array[minIndex] {
minIndex = i
}
return minIndex
}

for i in 0..<array.count {
let minPos = minIndex(array, startIndex: i)
if i != minPos {
swap(&array[i], &array[minPos])
}
}

return array
}





assert(selectionSort(["c", "a", "b"]).isSorted())

/*:
[Table of Contents](Table%20of%20Contents) | [Previous](@previous) | [Next](@next)
Original file line number Diff line number Diff line change
@@ -7,10 +7,29 @@ Also check what `@noescape` means in Swift.
****
Since you now use another function to do the comparison, you can remove the `Comparable` constraint.
*/

func selectionSort<T>(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..<array.count where isOrderedBefore(array[i], array[minIndex]) {
minIndex = i
}
return minIndex
}

for i in 0..<array.count {
let minPos = minIndex(array, startIndex: i, isOrderedBefore: isOrderedBefore)
if i != minPos {
swap(&array[i], &array[minPos])
}
}

return array
}


//assert(selectionSort([3, 1, 2], isOrderedBefore: <).isSorted())
assert(selectionSort([3, 1, 2], isOrderedBefore: <).isSorted())


/*:
Original file line number Diff line number Diff line change
@@ -9,8 +9,23 @@ Better define an inner function to find the next minimum in the array.

*/

func selectionSort(array: [Int]) -> [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..<array.count where array[i] < array[minIndex] {
minIndex = i
}
return minIndex
}

for i in 0..<array.count {
let minPos = minIndex(array, startIndex: i)
if i != minPos {
swap(&array[i], &array[minPos])
}
}

return array
}
@@ -20,8 +35,8 @@ func selectionSort(array: [Int]) -> [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)
Original file line number Diff line number Diff line change
@@ -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..<array.count where isOrderedBefore(array[i], array[minIndex]) {
minIndex = i
}
return minIndex
}

for i in 0..<array.count {
let minIndex = minIndex(array, startIndex: i, isOrderedBefore: isOrderedBefore)
if i != minIndex {
swap(&array[i], &array[minIndex])
}
}

return array
}
}

//assert([1, 3, 2].selectionSort(<).isSorted())
//assert([1: "b", 2: "a"].selectionSort({$0.0 > $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)