Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README.markdown for some places and Selection Sampling Content.swift improvement #997

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Count Occurrences/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func countOccurrences<T: Comparable>(of key: T, in array: [T]) -> Int {
var high = array.count
while low < high {
let midIndex = low + (high - low)/2
if a[midIndex] < key {
if array[midIndex] < key {
low = midIndex + 1
} else {
high = midIndex
Expand All @@ -42,7 +42,7 @@ func countOccurrences<T: Comparable>(of key: T, in array: [T]) -> Int {
var high = array.count
while low < high {
let midIndex = low + (high - low)/2
if a[midIndex] > key {
if array[midIndex] > key {
high = midIndex
} else {
low = midIndex + 1
Expand Down
2 changes: 1 addition & 1 deletion Kth Largest Element/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,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
6 changes: 6 additions & 0 deletions Selection Sampling/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ Let's say you have a deck of 52 playing cards and you need to draw 10 cards at r
Here's a very fast version:

```swift
public func random(min: Int, max: Int) -> Int {
assert(min < max)
return min + Int(arc4random_uniform(UInt32(max - min + 1)))
}


func select<T>(from a: [T], count k: Int) -> [T] {
var a = a
for i in 0..<k {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func select<T>(from a: [T], count k: Int) -> [T] {
for i in 0..<k {
let r = random(min: i, max: a.count - 1)
if i != r {
swap(&a[i], &a[r])
a.swapAt(i, r)
}
}
return Array(a[0..<k])
Expand Down