Skip to content

Commit 5eb8ad7

Browse files
authored
Merge pull request kodecocodes#689 from KeithMorning/orign_master
Fix some issues in Kth Largest Element && Selection Sampling article
2 parents 833c994 + 5beb84a commit 5eb8ad7

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

Binary Search Tree/README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ If there are no more nodes to look at -- when `left` or `right` is nil -- then w
268268
Searching is a recursive process, but you can also implement it with a simple loop instead:
269269

270270
```swift
271-
public func search(value: T) -> BinarySearchTree? {
271+
public func search(_ value: T) -> BinarySearchTree? {
272272
var node: BinarySearchTree? = self
273273
while let n = node {
274274
if value < n.value {

Kth Largest Element/README.markdown

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Now, all we must do is take the value at index `a.count - k`:
4040
a[a.count - k] = a[8 - 4] = a[4] = 9
4141
```
4242

43-
Of course, if you were looking for the k-th *smallest* element, you'd use `a[k]`.
43+
Of course, if you were looking for the k-th *smallest* element, you'd use `a[k-1]`.
4444

4545
## A faster solution
4646

@@ -84,29 +84,29 @@ The index of pivot `9` is 4, and that's exactly the *k* we're looking for. We're
8484
The following function implements these ideas:
8585

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

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

96-
func randomizedPartition<T: Comparable>(inout a: [T], _ low: Int, _ high: Int) -> Int {
96+
func randomizedPartition<T: Comparable>(_ a: inout [T], _ low: Int, _ high: Int) -> Int {
9797
let pivot = randomPivot(&a, low, high)
9898
var i = low
9999
for j in low..<high {
100100
if a[j] <= pivot {
101-
swap(&a, i, j)
101+
a.swapAt(i, j)
102102
i += 1
103103
}
104104
}
105-
swap(&a, i, high)
105+
a.swapAt(i, high)
106106
return i
107107
}
108108

109-
func randomizedSelect<T: Comparable>(inout a: [T], _ low: Int, _ high: Int, _ k: Int) -> T {
109+
func randomizedSelect<T: Comparable>(_ a: inout [T], _ low: Int, _ high: Int, _ k: Int) -> T {
110110
if low < high {
111111
let p = randomizedPartition(&a, low, high)
112112
if k == p {

Selection Sampling/README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ print(output.count)
193193

194194
The performance of this second algorithm is **O(n)** as it may require a pass through the entire input array.
195195

196-
> **Note:** If `k > n/2`, then it's more efficient to do it the other way around and choose `k` items to remove.
196+
> **Note:** If `k > n/2`, then it's more efficient to do it the other way around and choose `a.count - k` items to remove.
197197
198198
Based on code from Algorithm Alley, Dr. Dobb's Magazine, October 1993.
199199

0 commit comments

Comments
 (0)