@@ -5,7 +5,7 @@ import Foundation
5
5
6
6
// *** Simple but inefficient version of quicksort ***
7
7
8
- func quicksort< T: Comparable > ( a: [ T ] ) -> [ T ] {
8
+ func quicksort< T: Comparable > ( _ a: [ T ] ) -> [ T ] {
9
9
guard a. count > 1 else { return a }
10
10
11
11
let pivot = a [ a. count/ 2 ]
@@ -34,7 +34,7 @@ quicksort(list1)
34
34
partition is [low...p-1]; the right partition is [p+1...high], where p is the
35
35
return value.
36
36
*/
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 {
38
38
let pivot = a [ high]
39
39
40
40
var i = low
@@ -53,7 +53,7 @@ var list2 = [ 10, 0, 3, 9, 2, 14, 26, 27, 1, 5, 8, -1, 8 ]
53
53
partitionLomuto ( & list2, low: 0 , high: list2. count - 1 )
54
54
list2
55
55
56
- func quicksortLomuto< T: Comparable > ( inout a: [ T ] , low: Int , high: Int ) {
56
+ func quicksortLomuto< T: Comparable > ( _ a: inout [ T ] , low: Int , high: Int ) {
57
57
if low < high {
58
58
let p = partitionLomuto ( & a, low: low, high: high)
59
59
quicksortLomuto ( & a, low: low, high: p - 1 )
@@ -75,7 +75,7 @@ quicksortLomuto(&list2, low: 0, high: list2.count - 1)
75
75
where p is the return value. The pivot value is placed somewhere inside one
76
76
of the two partitions, but the algorithm doesn't tell you which one or where.
77
77
*/
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 {
79
79
let pivot = a [ low]
80
80
var i = low - 1
81
81
var j = high + 1
@@ -96,7 +96,7 @@ var list3 = [ 8, 0, 3, 9, 2, 14, 10, 27, 1, 5, 8, -1, 26 ]
96
96
partitionHoare ( & list3, low: 0 , high: list3. count - 1 )
97
97
list3
98
98
99
- func quicksortHoare< T: Comparable > ( inout a: [ T ] , low: Int , high: Int ) {
99
+ func quicksortHoare< T: Comparable > ( _ a: inout [ T ] , low: Int , high: Int ) {
100
100
if low < high {
101
101
let p = partitionHoare ( & a, low: low, high: high)
102
102
quicksortHoare ( & a, low: low, high: p)
@@ -111,12 +111,12 @@ quicksortHoare(&list3, low: 0, high: list3.count - 1)
111
111
// *** Randomized sorting ***
112
112
113
113
/* 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 {
115
115
assert ( min < max)
116
116
return min + Int( arc4random_uniform ( UInt32 ( max - min + 1 ) ) )
117
117
}
118
118
119
- func quicksortRandom< T: Comparable > ( inout a: [ T ] , low: Int , high: Int ) {
119
+ func quicksortRandom< T: Comparable > ( _ a: inout [ T ] , low: Int , high: Int ) {
120
120
if low < high {
121
121
let pivotIndex = random ( min: low, max: high)
122
122
( a [ pivotIndex] , a [ high] ) = ( a [ high] , a [ pivotIndex] )
@@ -139,7 +139,7 @@ list4
139
139
Swift's swap() doesn't like it if the items you're trying to swap refer to
140
140
the same memory location. This little wrapper simply ignores such swaps.
141
141
*/
142
- public func swap< T> ( inout a: [ T ] , _ i: Int , _ j: Int ) {
142
+ public func swap< T> ( _ a: inout [ T ] , _ i: Int , _ j: Int ) {
143
143
if i != j {
144
144
swap ( & a[ i] , & a[ j] )
145
145
}
@@ -149,7 +149,7 @@ public func swap<T>(inout a: [T], _ i: Int, _ j: Int) {
149
149
Dutch national flag partitioning.
150
150
Returns a tuple with the start and end index of the middle area.
151
151
*/
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 ) {
153
153
let pivot = a [ pivotIndex]
154
154
155
155
var smaller = low
@@ -175,7 +175,7 @@ var list5 = [ 10, 0, 3, 9, 2, 14, 8, 27, 1, 5, 8, -1, 26 ]
175
175
partitionDutchFlag ( & list5, low: 0 , high: list5. count - 1 , pivotIndex: 10 )
176
176
list5
177
177
178
- func quicksortDutchFlag< T: Comparable > ( inout a: [ T ] , low: Int , high: Int ) {
178
+ func quicksortDutchFlag< T: Comparable > ( _ a: inout [ T ] , low: Int , high: Int ) {
179
179
if low < high {
180
180
let pivotIndex = random ( min: low, max: high)
181
181
let ( p, q) = partitionDutchFlag ( & a, low: low, high: high, pivotIndex: pivotIndex)
0 commit comments