Skip to content

Commit

Permalink
Update README.markdown
Browse files Browse the repository at this point in the history
update the code in the article same as source code
  • Loading branch information
KeithMorning authored Jan 29, 2018
1 parent 157431e commit e14a46f
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions Kth Largest Element/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,29 @@ The index of pivot `9` is 4, and that's exactly the *k* we're looking for. We're
The following function implements these ideas:

```swift
public func randomizedSelect<T: Comparable>(array: [T], order k: Int) -> T {
public func randomizedSelect<T: Comparable>(_ array: [T], order k: Int) -> T {
var a = array

func randomPivot<T: Comparable>(inout a: [T], _ low: Int, _ high: Int) -> T {
func randomPivot<T: Comparable>(_ a: inout [T], _ low: Int, _ high: Int) -> T {
let pivotIndex = random(min: low, max: high)
swap(&a, pivotIndex, high)
a.swapAt(pivotIndex, high)
return a[high]
}

func randomizedPartition<T: Comparable>(inout a: [T], _ low: Int, _ high: Int) -> Int {
func randomizedPartition<T: Comparable>(_ a: inout [T], _ low: Int, _ high: Int) -> Int {
let pivot = randomPivot(&a, low, high)
var i = low
for j in low..<high {
if a[j] <= pivot {
swap(&a, i, j)
a.swapAt(i, j)
i += 1
}
}
swap(&a, i, high)
a.swapAt(i, high)
return i
}

func randomizedSelect<T: Comparable>(inout a: [T], _ low: Int, _ high: Int, _ k: Int) -> T {
func randomizedSelect<T: Comparable>(_ a: inout [T], _ low: Int, _ high: Int, _ k: Int) -> T {
if low < high {
let p = randomizedPartition(&a, low, high)
if k == p {
Expand Down

0 comments on commit e14a46f

Please sign in to comment.