8000 Removing "a" label from binarySearch method in playground so it match… · codee/swift-algorithm-club@20236b2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 20236b2

Browse files
author
Chris Pilcher
committed
Removing "a" label from binarySearch method in playground so it matches BinarySearch.swift. Applying same change to readme.
1 parent 19cc32e commit 20236b2

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

Binary Search/BinarySearch.playground/Contents.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ let numbers = [11, 59, 3, 2, 53, 17, 31, 7, 19, 67, 47, 13, 37, 61, 29, 43, 5, 4
77
let sorted = numbers.sorted()
88

99
// Using recursive solution
10-
binarySearch(a: sorted, key: 2, range: 0 ..< sorted.count) // gives 0
11-
binarySearch(a: sorted, key: 67, range: 0 ..< sorted.count) // gives 18
12-
binarySearch(a: sorted, key: 43, range: 0 ..< sorted.count) // gives 13
13-
binarySearch(a: sorted, key: 42, range: 0 ..< sorted.count) // nil
10+
binarySearch(sorted, key: 2, range: 0 ..< sorted.count) // gives 0
11+
binarySearch(sorted, key: 67, range: 0 ..< sorted.count) // gives 18
12+
binarySearch(sorted, key: 43, range: 0 ..< sorted.count) // gives 13
13+
binarySearch(sorted, key: 42, range: 0 ..< sorted.count) // nil
1414

1515
// Using iterative solution
16-
binarySearch(a: sorted, key: 2) // gives 0
17-
binarySearch(a: sorted, key: 67) // gives 18
18-
binarySearch(a: sorted, key: 43) // gives 13
19-
binarySearch(a: sorted, key: 42) // nil
16+
binarySearch(sorted, key: 2) // gives 0
17+
binarySearch(sorted, key: 67) // gives 18
18+
binarySearch(sorted, key: 43) // gives 13
19+
binarySearch(sorted, key: 42) // nil

Binary Search/BinarySearch.playground/Sources/BinarySearch.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ import Foundation
1313

1414
// The recursive version of binary search.
1515

16-
public func binarySearch<T: Comparable>(a: [T], key: T, range: Range<Int>) -> Int? {
16+
public func binarySearch<T: Comparable>(_ a: [T], key: T, range: Range<Int>) -> Int? {
1717
if range.lowerBound >= range.upperBound {
1818
return nil
1919
} else {
2020
let midIndex = range.lowerBound + (range.upperBound - range.lowerBound) / 2
2121
if a[midIndex] > key {
22-
return binarySearch(a: a, key: key, range: range.lowerBound ..< midIndex)
22+
return binarySearch(a, key: key, range: range.lowerBound ..< midIndex)
2323
} else if a[midIndex] < key {
24-
return binarySearch(a: a, key: key, range: midIndex + 1 ..< range.upperBound)
24+
return binarySearch(a, key: key, range: midIndex + 1 ..< range.upperBound)
2525
} else {
2626
return midIndex
2727
}
@@ -35,7 +35,7 @@ public func binarySearch<T: Comparable>(a: [T], key: T, range: Range<Int>) -> In
3535
uses a while loop, while the other calls itself recursively.
3636
**/
3737

38-
public func binarySearch<T: Comparable>(a: [T], key: T) -> Int? {
38+
public func binarySearch<T: Comparable>(_ a: [T], key: T) -> Int? {
3939
var lowerBound = 0
4040
var upperBound = a.count
4141
while lowerBound < upperBound {

Binary Search/README.markdown

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ numbers.indexOf(43) // returns 15
1515
The built-in `indexOf()` function performs a [linear search](../Linear Search/). In code that looks something like this:
1616

1717
```swift
18-
func linearSearch<T: Equatable>(a: [T], _ key: T) -> Int? {
18+
func linearSearch<T: Equatable>(_ a: [T], _ key: T) -> Int? {
1919
for i in 0 ..< a.count {
2020
if a[i] == key {
2121
return i
@@ -45,7 +45,7 @@ Sounds great, but there is a downside to using binary search: the array must be
4545

4646
Here's how binary search works:
4747

48-
- Split the array in half and determine whether the thing you're looking for, known as the *search key*, is in the left half or in the right half.
48+
- Split the array in half and determine whether the thing you're looking for, known as the *search key*, is in the left half or in the right half.
4949
- How do you determine in which half the search key is? This is why you sorted the array first, so you can do a simple `<` or `>` comparison.
5050
- If the search key is in the left half, you repeat the process there: split the left half into two even smaller pieces and look in which piece the search key must lie. (Likewise for when it's the right half.)
5151
- This repeats until the search key is found. If the array cannot be split up any further, you must regrettably conclude that the search key is not present in the array.
@@ -57,7 +57,7 @@ Now you know why it's called a "binary" search: in every step it splits the arra
5757
Here is a recursive implementation of binary search in Swift:
5858

5959
```swift
60-
func binarySearch<T: Comparable>(a: [T], key: T, range: Range<Int>) -> Int? {
60+
func binarySearch<T: Comparable>(_ a: [T], key: T, range: Range<Int>) -> Int? {
6161
if range.lowerBound >= range.upperBound {
6262
// If we get here, then the search key is not present in the array.
6363
return nil
@@ -180,7 +180,7 @@ Binary search is recursive in nature because you apply the same logic over and o
180180
Here is an iterative implementation of binary search in Swift:
181181

182182
```swift
183-
func binarySearch<T: Comparable>(a: [T], key: T) -> Int? {
183+
func binarySearch<T: Comparable>(_ a: [T], key: T) -> Int? {
184184
var lowerBound = 0
185185
var upperBound = a.count
186186
while lowerBound < upperBound {

0 commit comments

Comments
 (0)
0