Skip to content

Commit df44023

Browse files
committed
use swift guard statement consistently
1 parent e044dd7 commit df44023

File tree

4 files changed

+27
-37
lines changed

4 files changed

+27
-37
lines changed

Quicksort/Quicksort.playground/Contents.swift

+11-13
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@ import Foundation
66
// *** Simple but inefficient version of quicksort ***
77

88
func quicksort<T: Comparable>(a: [T]) -> [T] {
9-
if a.count <= 1 {
10-
return a
11-
} else {
12-
let pivot = a[a.count/2]
13-
let less = a.filter { $0 < pivot }
14-
let equal = a.filter { $0 == pivot }
15-
let greater = a.filter { $0 > pivot }
16-
17-
// Uncomment this following line to see in detail what the
18-
// pivot is in each step and how the subarrays are partitioned.
19-
//print(pivot, less, equal, greater)
9+
guard a.count > 1 else { return a }
10+
11+
let pivot = a[a.count/2]
12+
let less = a.filter { $0 < pivot }
13+
let equal = a.filter { $0 == pivot }
14+
let greater = a.filter { $0 > pivot }
15+
16+
// Uncomment this following line to see in detail what the
17+
// pivot is in each step and how the subarrays are partitioned.
18+
//print(pivot, less, equal, greater) return quicksort(less) + equal + quicksort(greater)
2019

21-
return quicksort(less) + equal + quicksort(greater)
22-
}
20+
return quicksort(less) + equal + quicksort(greater)
2321
}
2422

2523
let list1 = [ 10, 0, 3, 9, 2, 14, 8, 27, 1, 5, 8, -1, 26 ]

Quicksort/Quicksort.playground/timeline.xctimeline

-6
This file was deleted.

Quicksort/Quicksort.swift

+8-9
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import Foundation
44
Easy to understand but not very efficient.
55
*/
66
func quicksort<T: Comparable>(a: [T]) -> [T] {
7-
if a.count <= 1 {
8-
return a
9-
} else {
10-
let pivot = a[a.count/2]
11-
let less = a.filter { $0 < pivot }
12-
let equal = a.filter { $0 == pivot }
13-
let greater = a.filter { $0 > pivot }
14-
return quicksort(less) + equal + quicksort(greater)
15-
}
7+
guard a.count > 1 else { return a }
8+
9+
let pivot = a[a.count/2]
10+
let less = a.filter { $0 < pivot }
11+
let equal = a.filter { $0 == pivot }
12+
let greater = a.filter { $0 > pivot }
13+
14+
return quicksort(less) + equal + quicksort(greater)
1615
}
1716

1817
// MARK: - Lomuto

Quicksort/README.markdown

+8-9
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ Here's an implementation in Swift that should be easy to understand:
88

99
```swift
1010
func quicksort<T: Comparable>(a: [T]) -> [T] {
11-
if a.count <= 1 {
12-
return a
13-
} else {
14-
let pivot = a[a.count/2]
15-
let less = a.filter { $0 < pivot }
16-
let equal = a.filter { $0 == pivot }
17-
let greater = a.filter { $0 > pivot }
18-
return quicksort(less) + equal + quicksort(greater)
19-
}
11+
guard a.count > 1 else { return a }
12+
13+
let pivot = a[a.count/2]
14+
let less = a.filter { $0 < pivot }
15+
let equal = a.filter { $0 == pivot }
16+
let greater = a.filter { $0 > pivot }
17+
18+
return quicksort(less) + equal + quicksort(greater)
2019
}
2120
```
2221

0 commit comments

Comments
 (0)