From 62d3a1a8806882f8910bc8d030973878f982cde8 Mon Sep 17 00:00:00 2001 From: efemaz Date: Mon, 4 Apr 2022 01:08:32 +0300 Subject: [PATCH 1/5] Update README.markdown --- Count Occurrences/README.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Count Occurrences/README.markdown b/Count Occurrences/README.markdown index 8e935ea58..95f020e90 100644 --- a/Count Occurrences/README.markdown +++ b/Count Occurrences/README.markdown @@ -28,7 +28,7 @@ func countOccurrences(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 @@ -42,7 +42,7 @@ func countOccurrences(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 From 82f8036304a10f3eff914348bcd78d48a3bd5bd0 Mon Sep 17 00:00:00 2001 From: efemaz Date: Mon, 4 Apr 2022 01:30:33 +0300 Subject: [PATCH 2/5] Update README.markdown --- Kth Largest Element/README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Kth Largest Element/README.markdown b/Kth Largest Element/README.markdown index b23d22b74..92839469a 100644 --- a/Kth Largest Element/README.markdown +++ b/Kth Largest Element/README.markdown @@ -88,7 +88,7 @@ public func randomizedSelect(_ array: [T], order k: Int) -> T { var a = array func randomPivot(_ a: inout [T], _ low: Int, _ high: Int) -> T { - let pivotIndex = random(min: low, max: high) + let pivotIndex = Int.random(min: low, max: high) a.swapAt(pivotIndex, high) return a[high] } From 8105b70ccc5f80a907ba772eba5f05b1bea82653 Mon Sep 17 00:00:00 2001 From: efemaz Date: Mon, 4 Apr 2022 01:32:25 +0300 Subject: [PATCH 3/5] Update README.markdown --- Kth Largest Element/README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Kth Largest Element/README.markdown b/Kth Largest Element/README.markdown index 92839469a..7e60a8d4a 100644 --- a/Kth Largest Element/README.markdown +++ b/Kth Largest Element/README.markdown @@ -88,7 +88,7 @@ public func randomizedSelect(_ array: [T], order k: Int) -> T { var a = array func randomPivot(_ a: inout [T], _ low: Int, _ high: Int) -> T { - let pivotIndex = Int.random(min: low, max: high) + let pivotIndex = Int.random(in: low...high) a.swapAt(pivotIndex, high) return a[high] } From c8f761736a7377edde64c5a762a1a91c2d3044a0 Mon Sep 17 00:00:00 2001 From: efemaz Date: Mon, 4 Apr 2022 10:52:00 +0300 Subject: [PATCH 4/5] Update README.markdown --- Selection Sampling/README.markdown | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Selection Sampling/README.markdown b/Selection Sampling/README.markdown index 741f592f4..11465b90e 100644 --- a/Selection Sampling/README.markdown +++ b/Selection Sampling/README.markdown @@ -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(from a: [T], count k: Int) -> [T] { var a = a for i in 0.. Date: Mon, 4 Apr 2022 10:54:04 +0300 Subject: [PATCH 5/5] Update Contents.swift --- Selection Sampling/SelectionSampling.playground/Contents.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Selection Sampling/SelectionSampling.playground/Contents.swift b/Selection Sampling/SelectionSampling.playground/Contents.swift index 7443db67c..81989b8a3 100644 --- a/Selection Sampling/SelectionSampling.playground/Contents.swift +++ b/Selection Sampling/SelectionSampling.playground/Contents.swift @@ -19,7 +19,7 @@ func select(from a: [T], count k: Int) -> [T] { for i in 0..