Skip to content

Commit

Permalink
[Swift 4] Update Kth Largest Element
Browse files Browse the repository at this point in the history
  • Loading branch information
remlostime committed Jul 31, 2017
1 parent 7077bc1 commit b0e6897
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 91 deletions.
72 changes: 4 additions & 68 deletions Kth Largest Element/kthLargest.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
//: Playground - noun: a place where people can play

func kthLargest(_ a: [Int], _ k: Int) -> Int? {
let len = a.count
if k > 0 && k <= len {
let sorted = a.sorted()
return sorted[len - k]
} else {
return nil
}
}
// last checked with Xcode 9.0b4
#if swift(>=4.0)
print("Hello, Swift 4!")
#endif

let a = [5, 1, 3, 2, 7, 6, 4]

Expand All @@ -22,65 +17,6 @@ kthLargest(a, 6)
kthLargest(a, 7)
kthLargest(a, 8)

import Foundation

/* 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)))
}

/*
Swift's swap() doesn't like it if the items you're trying to swap refer to
the same memory location. This little wrapper simply ignores such swaps.
*/
public func swap<T>(_ a:inout [T], _ i: Int, _ j: Int) {
if i != j {
swap(&a[i], &a[j])
}
}

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)
swap(&a, pivotIndex, high)
return a[high]
}

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)
i += 1
}
}
swap(&a, i, high)
return i
}

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 {
return a[p]
} else if k < p {
return randomizedSelect(&a, low, p - 1, k)
} else {
return randomizedSelect(&a, p + 1, high, k)
}
} else {
return a[low]
}
}

precondition(a.count > 0)
return randomizedSelect(&a, 0, a.count - 1, k)
}

randomizedSelect(a, order: 0)
randomizedSelect(a, order: 1)
randomizedSelect(a, order: 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import Foundation
Returns the k-th largest value inside of an array a.
This is an O(n log n) solution since we sort the array.
*/
func kthLargest(a: [Int], k: Int) -> Int? {
public func kthLargest(_ a: [Int], _ k: Int) -> Int? {
let len = a.count
if k > 0 && k <= len {
let sorted = a.sort()
let sorted = a.sorted()
return sorted[len - k]
} else {
return nil
Expand All @@ -17,21 +17,11 @@ func kthLargest(a: [Int], k: Int) -> Int? {
// MARK: - Randomized selection

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

/*
Swift's swap() doesn't like it if the items you're trying to swap refer to
the same memory location. This little wrapper simply ignores such swaps.
*/
public func swap<T>(inout a: [T], _ i: Int, _ j: Int) {
if i != j {
swap(&a[i], &a[j])
}
}

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

Expand All @@ -45,29 +35,29 @@ public func swap<T>(inout a: [T], _ i: Int, _ j: Int) {

Expected running time: O(n) if the elements are distinct.
*/
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
3 changes: 0 additions & 3 deletions swift-algorithm-club.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b0e6897

Please sign in to comment.