Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Refactor selection sort to sort in place #1018

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
21 changes: 9 additions & 12 deletions Selection Sort/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -59,40 +59,37 @@ The selection sort is an *in-place* sort because everything happens in the same
Here is an implementation of selection sort in Swift:

```swift
func selectionSort(_ array: [Int]) -> [Int] {
guard array.count > 1 else { return array } // 1
func selectionSort(_ array: inout [Int]){ // 1
guard array.count > 1 else { return array } // 2

var a = array // 2

for x in 0 ..< a.count - 1 { // 3
for x in 0 ..< array.count - 1 { // 3

var lowest = x
for y in x + 1 ..< a.count { // 4
if a[y] < a[lowest] {
for y in x + 1 ..< array.count { // 4
if array[y] < array[lowest] {
lowest = y
}
}

if x != lowest { // 5
a.swapAt(x, lowest)
array.swapAt(x, lowest)
}
}
return a
}
```

Put this code in a playground and test it like so:

```swift
let list = [ 10, -1, 3, 9, 2, 27, 8, 5, 1, 3, 0, 26 ]
selectionSort(list)
selectionSort(&list)
```

A step-by-step explanation of how the code works:

1. If the array is empty or only contains a single element, then there is no need to sort.
1. By adding `inout` before the parameter name, you indicate that the function can modify the original array directly.

2. Make a copy of the array. This is necessary because we cannot modify the contents of the `array` parameter directly in Swift. Like the Swift's `sort()` function, the `selectionSort()` function will return a sorted *copy* of the original array.
2. If the array is empty or only contains a single element, then there is no need to sort.

3. There are two loops inside this function. The outer loop looks at each of the elements in the array in turn; this is what moves the `|` bar forward.

Expand Down
6 changes: 3 additions & 3 deletions Selection Sort/SelectionSort.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//: Playground - noun: a place where people can play

let list = [ 10, -1, 3, 9, 2, 27, 8, 5, 1, 3, 0, 26 ]
selectionSort(list)
selectionSort(list, <)
selectionSort(list, >)
selectionSort(&list)
selectionSort(&list, <)
selectionSort(&list, >)
12 changes: 5 additions & 7 deletions Selection Sort/SelectionSort.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
public func selectionSort<T: Comparable>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
public func selectionSort<T: Comparable>(_ array: inout [T], _ isOrderedBefore: (T, T) -> Bool) {
guard array.count > 1 else { return array }

var a = array
for x in 0 ..< a.count - 1 {
for x in 0 ..< array.count - 1 {

// Find the lowest value in the rest of the array.
var lowest = x
for y in x + 1 ..< a.count {
if isOrderedBefore(a[y], a[lowest]) {
for y in x + 1 ..< array.count {
if isOrderedBefore(array[y], array[lowest]) {
lowest = y
}
}

// Swap the lowest value with the current array index.
if x != lowest {
a.swapAt(x, lowest)
array.swapAt(x, lowest)
}
}
return a
}