Skip to content

Commit 7e72285

Browse files
authored
Merge pull request kodecocodes#217 from raywenderlich/Swift3Quicksort
Swift3 migration for QuickSort
2 parents 1082749 + 35cca84 commit 7e72285

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

Quicksort/Quicksort.playground/Contents.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Foundation
55

66
// *** Simple but inefficient version of quicksort ***
77

8-
func quicksort<T: Comparable>(a: [T]) -> [T] {
8+
func quicksort<T: Comparable>(_ a: [T]) -> [T] {
99
guard a.count > 1 else { return a }
1010

1111
let pivot = a[a.count/2]
@@ -34,7 +34,7 @@ quicksort(list1)
3434
partition is [low...p-1]; the right partition is [p+1...high], where p is the
3535
return value.
3636
*/
37-
func partitionLomuto<T: Comparable>(inout a: [T], low: Int, high: Int) -> Int {
37+
func partitionLomuto<T: Comparable>(_ a: inout [T], low: Int, high: Int) -> Int {
3838
let pivot = a[high]
3939

4040
var i = low
@@ -53,7 +53,7 @@ var list2 = [ 10, 0, 3, 9, 2, 14, 26, 27, 1, 5, 8, -1, 8 ]
5353
partitionLomuto(&list2, low: 0, high: list2.count - 1)
5454
list2
5555

56-
func quicksortLomuto<T: Comparable>(inout a: [T], low: Int, high: Int) {
56+
func quicksortLomuto<T: Comparable>(_ a: inout [T], low: Int, high: Int) {
5757
if low < high {
5858
let p = partitionLomuto(&a, low: low, high: high)
5959
quicksortLomuto(&a, low: low, high: p - 1)
@@ -75,7 +75,7 @@ quicksortLomuto(&list2, low: 0, high: list2.count - 1)
7575
where p is the return value. The pivot value is placed somewhere inside one
7676
of the two partitions, but the algorithm doesn't tell you which one or where.
7777
*/
78-
func partitionHoare<T: Comparable>(inout a: [T], low: Int, high: Int) -> Int {
78+
func partitionHoare<T: Comparable>(_ a: inout [T], low: Int, high: Int) -> Int {
7979
let pivot = a[low]
8080
var i = low - 1
8181
var j = high + 1
@@ -96,7 +96,7 @@ var list3 = [ 8, 0, 3, 9, 2, 14, 10, 27, 1, 5, 8, -1, 26 ]
9696
partitionHoare(&list3, low: 0, high: list3.count - 1)
9797
list3
9898

99-
func quicksortHoare<T: Comparable>(inout a: [T], low: Int, high: Int) {
99+
func quicksortHoare<T: Comparable>(_ a: inout [T], low: Int, high: Int) {
100100
if low < high {
101101
let p = partitionHoare(&a, low: low, high: high)
102102
quicksortHoare(&a, low: low, high: p)
@@ -111,12 +111,12 @@ quicksortHoare(&list3, low: 0, high: list3.count - 1)
111111
// *** Randomized sorting ***
112112

113113
/* Returns a random integer in the range min...max, inclusive. */
114-
public func random(min min: Int, max: Int) -> Int {
114+
public func random(min: Int, max: Int) -> Int {
115115
assert(min < max)
116116
return min + Int(arc4random_uniform(UInt32(max - min + 1)))
117117
}
118118

119-
func quicksortRandom<T: Comparable>(inout a: [T], low: Int, high: Int) {
119+
func quicksortRandom<T: Comparable>(_ a: inout [T], low: Int, high: Int) {
120120
if low < high {
121121
let pivotIndex = random(min: low, max: high)
122122
(a[pivotIndex], a[high]) = (a[high], a[pivotIndex])
@@ -139,7 +139,7 @@ list4
139139
Swift's swap() doesn't like it if the items you're trying to swap refer to
140140
the same memory location. This little wrapper simply ignores such swaps.
141141
*/
142-
public func swap<T>(inout a: [T], _ i: Int, _ j: Int) {
142+
public func swap<T>(_ a: inout [T], _ i: Int, _ j: Int) {
143143
if i != j {
144144
swap(&a[i], &a[j])
145145
}
@@ -149,7 +149,7 @@ public func swap<T>(inout a: [T], _ i: Int, _ j: Int) {
149149
Dutch national flag partitioning.
150150
Returns a tuple with the start and end index of the middle area.
151151
*/
152-
func partitionDutchFlag<T: Comparable>(inout a: [T], low: Int, high: Int, pivotIndex: Int) -> (Int, Int) {
152+
func partitionDutchFlag<T: Comparable>(_ a: inout [T], low: Int, high: Int, pivotIndex: Int) -> (Int, Int) {
153153
let pivot = a[pivotIndex]
154154

155155
var smaller = low
@@ -175,7 +175,7 @@ var list5 = [ 10, 0, 3, 9, 2, 14, 8, 27, 1, 5, 8, -1, 26 ]
175175
partitionDutchFlag(&list5, low: 0, high: list5.count - 1, pivotIndex: 10)
176176
list5
177177

178-
func quicksortDutchFlag<T: Comparable>(inout a: [T], low: Int, high: Int) {
178+
func quicksortDutchFlag<T: Comparable>(_ a: inout [T], low: Int, high: Int) {
179179
if low < high {
180180
let pivotIndex = random(min: low, max: high)
181181
let (p, q) = partitionDutchFlag(&a, low: low, high: high, pivotIndex: pivotIndex)

Quicksort/README.markdown

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Quicksort is one of the most famous algorithms in history. It was invented way b
77
Here's an implementation in Swift that should be easy to understand:
88

99
```swift
10-
func quicksort<T: Comparable>(a: [T]) -> [T] {
10+
func quicksort<T: Comparable>(_ a: [T]) -> [T] {
1111
guard a.count > 1 else { return a }
1212

1313
let pivot = a[a.count/2]
@@ -131,7 +131,7 @@ In the first example of quicksort I showed you, partitioning was done by calling
131131
Here's an implementation of Lomuto's partitioning scheme in Swift:
132132

133133
```swift
134-
func partitionLomuto<T: Comparable>(inout a: [T], low: Int, high: Int) -> Int {
134+
func partitionLomuto<T: Comparable>(_ a: inout [T], low: Int, high: Int) -> Int {
135135
let pivot = a[high]
136136

137137
var i = low
@@ -250,7 +250,7 @@ And we return `i`, the index of the pivot element.
250250
Let's use this partitioning scheme to build quicksort. Here's the code:
251251

252252
```swift
253-
func quicksortLomuto<T: Comparable>(inout a: [T], low: Int, high: Int) {
253+
func quicksortLomuto<T: Comparable>(_ a: inout [T], low: Int, high: Int) {
254254
if low < high {
255255
let p = partitionLomuto(&a, low: low, high: high)
256256
quicksortLomuto(&a, low: low, high: p - 1)
@@ -277,7 +277,7 @@ This partitioning scheme is by Hoare, the inventor of quicksort.
277277
Here is the code:
278278

279279
```Swift
280-
func partitionHoare<T: Comparable>(inout a: [T], low: Int, high: Int) -> Int {
280+
func partitionHoare<T: Comparable>(_ a: inout [T], low: Int, high: Int) -> Int {
281281
let pivot = a[low]
282282
var i = low - 1
283283
var j = high + 1
@@ -318,7 +318,7 @@ The pivot is placed somewhere inside one of the two partitions, but the algorith
318318
Because of these differences, the implementation of Hoare's quicksort is slightly different:
319319

320320
```swift
321-
func quicksortHoare<T: Comparable>(inout a: [T], low: Int, high: Int) {
321+
func quicksortHoare<T: Comparable>(_ a: inout [T], low: Int, high: Int) {
322322
if low < high {
323323
let p = partitionHoare(&a, low: low, high: high)
324324
quicksortHoare(&a, low: low, high: p)
@@ -380,7 +380,7 @@ Another common solution is to choose the pivot randomly. Sometimes this may resu
380380
Here is how you can do quicksort with a randomly chosen pivot:
381381

382382
```swift
383-
func quicksortRandom<T: Comparable>(inout a: [T], low: Int, high: Int) {
383+
func quicksortRandom<T: Comparable>(_ a: inout [T], low: Int, high: Int) {
384384
if low < high {
385385
let pivotIndex = random(min: low, max: high) // 1
386386

@@ -412,7 +412,7 @@ But as you've seen with the Lomuto partitioning scheme, if the pivot occurs more
412412
The code for this scheme is:
413413

414414
```swift
415-
func partitionDutchFlag<T: Comparable>(inout a: [T], low: Int, high: Int, pivotIndex: Int) -> (Int, Int) {
415+
func partitionDutchFlag<T: Comparable>(_ a: inout [T], low: Int, high: Int, pivotIndex: Int) -> (Int, Int) {
416416
let pivot = a[pivotIndex]
417417

418418
var smaller = low
@@ -461,7 +461,7 @@ Notice how the two `8`s are in the middle now. The return value from `partitionD
461461
Here is how you would use it in quicksort:
462462

463463
```swift
464-
func quicksortDutchFlag<T: Comparable>(inout a: [T], low: Int, high: Int) {
464+
func quicksortDutchFlag<T: Comparable>(_ a: inout [T], low: Int, high: Int) {
465465
if low < high {
466466
let pivotIndex = random(min: low, max: high)
467467
let (p, q) = partitionDutchFlag(&a, low: low, high: high, pivotIndex: pivotIndex)

0 commit comments

Comments
 (0)