From 27189694c1956c2a9b809707f5bade32f65cffff Mon Sep 17 00:00:00 2001 From: Ibrahim Hamed <55464011+ibrahimHamed@users.noreply.github.com> Date: Sat, 14 Oct 2023 23:46:21 +0300 Subject: [PATCH 1/3] Update README.markdown --- Selection Sort/README.markdown | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/Selection Sort/README.markdown b/Selection Sort/README.markdown index b958da927..153e84a05 100644 --- a/Selection Sort/README.markdown +++ b/Selection Sort/README.markdown @@ -59,25 +59,22 @@ 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 } ``` @@ -85,14 +82,14 @@ 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. From 6350c33f2a16aa76eba447941558c35909be1580 Mon Sep 17 00:00:00 2001 From: Ibrahim Hamed <55464011+ibrahimHamed@users.noreply.github.com> Date: Sat, 14 Oct 2023 23:59:08 +0300 Subject: [PATCH 2/3] Update SelectionSort.swift --- Selection Sort/SelectionSort.swift | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Selection Sort/SelectionSort.swift b/Selection Sort/SelectionSort.swift index 943ae628d..4a08186d2 100644 --- a/Selection Sort/SelectionSort.swift +++ b/Selection Sort/SelectionSort.swift @@ -1,21 +1,19 @@ -public func selectionSort(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] { +public func selectionSort(_ 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 } From 019047adfdc0fb4800dded09f182eb6cf8bfe6c2 Mon Sep 17 00:00:00 2001 From: Ibrahim Hamed <55464011+ibrahimHamed@users.noreply.github.com> Date: Sat, 14 Oct 2023 23:59:57 +0300 Subject: [PATCH 3/3] Update Contents.swift --- Selection Sort/SelectionSort.playground/Contents.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Selection Sort/SelectionSort.playground/Contents.swift b/Selection Sort/SelectionSort.playground/Contents.swift index a552c9edb..f3ea0eeb5 100644 --- a/Selection Sort/SelectionSort.playground/Contents.swift +++ b/Selection Sort/SelectionSort.playground/Contents.swift @@ -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, >)