@@ -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 ]
5353partitionLomuto ( & list2, low: 0 , high: list2. count - 1 )
5454list2
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 ]
9696partitionHoare ( & list3, low: 0 , high: list3. count - 1 )
9797list3
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 ]
175175partitionDutchFlag ( & list5, low: 0 , high: list5. count - 1 , pivotIndex: 10 )
176176list5
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)
0 commit comments