Skip to content

Commit

Permalink
Kth Largest Element: updated to use Int.random API which has been int…
Browse files Browse the repository at this point in the history
…roduced with Swift 4.2
  • Loading branch information
Priyanka- authored and Priyanka- committed Mar 24, 2019
1 parent 791cc66 commit 7405c64
Showing 1 changed file with 1 addition and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ public func kthLargest(_ a: [Int], _ k: Int) -> Int? {

// MARK: - Randomized selection

/* Returns a random integer in the range min...max, inclusive. */
public func random(min: Int, max: Int) -> Int {
assert(min < max)
return min + Int(arc4random_uniform(UInt32(max - min + 1)))
}

/*
Returns the i-th smallest element from the array.

Expand All @@ -39,7 +33,7 @@ public func randomizedSelect<T: Comparable>(_ array: [T], order k: Int) -> T {
var a = array

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

0 comments on commit 7405c64

Please sign in to comment.