@@ -84,29 +84,29 @@ The index of pivot `9` is 4, and that's exactly the *k* we're looking for. We're
84
84
The following function implements these ideas:
85
85
86
86
``` swift
87
- public func randomizedSelect <T : Comparable >(array : [T], order k : Int ) -> T {
87
+ public func randomizedSelect <T : Comparable >(_ array : [T], order k : Int ) -> T {
88
88
var a = array
89
89
90
- func randomPivot <T : Comparable >(inout a : [T], _ low : Int , _ high : Int ) -> T {
90
+ func randomPivot <T : Comparable >(_ a : inout [T], _ low : Int , _ high : Int ) -> T {
91
91
let pivotIndex = random (min : low, max : high)
92
- swap ( & a, pivotIndex, high)
92
+ a. swapAt ( pivotIndex, high)
93
93
return a[high]
94
94
}
95
95
96
- func randomizedPartition <T : Comparable >(inout a : [T], _ low : Int , _ high : Int ) -> Int {
96
+ func randomizedPartition <T : Comparable >(_ a : inout [T], _ low : Int , _ high : Int ) -> Int {
97
97
let pivot = randomPivot (& a, low, high)
98
98
var i = low
99
99
for j in low..< high {
100
100
if a[j] <= pivot {
101
- swap ( & a, i, j)
101
+ a. swapAt ( i, j)
102
102
i += 1
103
103
}
104
104
}
105
- swap ( & a, i, high)
105
+ a. swapAt ( i, high)
106
106
return i
107
107
}
108
108
109
- func randomizedSelect <T : Comparable >(inout a : [T], _ low : Int , _ high : Int , _ k : Int ) -> T {
109
+ func randomizedSelect <T : Comparable >(_ a : inout [T], _ low : Int , _ high : Int , _ k : Int ) -> T {
110
110
if low < high {
111
111
let p = randomizedPartition (& a, low, high)
112
112
if k == p {
0 commit comments