Skip to content

Commit fba86e1

Browse files
committed
Fix code indentation on Binary Search readme
1 parent aec3405 commit fba86e1

File tree

1 file changed

+43
-43
lines changed

1 file changed

+43
-43
lines changed

Binary Search/README.markdown

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ The built-in `indexOf()` function performs a [linear search](../Linear Search/).
1616

1717
```swift
1818
func linearSearch<T: Equatable>(a: [T], _ key: T) -> Int? {
19-
for i in 0 ..< a.count {
20-
if a[i] == key {
21-
return i
22-
}
23-
}
24-
return nil
19+
for i in 0 ..< a.count {
20+
if a[i] == key {
21+
return i
22+
}
23+
}
24+
return nil
2525
}
2626
```
2727

@@ -58,27 +58,27 @@ Here is a recursive implementation of binary search in Swift:
5858

5959
```swift
6060
func binarySearch<T: Comparable>(a: [T], key: T, range: Range<Int>) -> Int? {
61-
if range.lowerBound >= range.upperBound {
62-
// If we get here, then the search key is not present in the array.
63-
return nil
64-
65-
} else {
66-
// Calculate where to split the array.
67-
let midIndex = range.lowerBound + (range.upperBound - range.lowerBound) / 2
68-
69-
// Is the search key in the left half?
70-
if a[midIndex] > key {
71-
return binarySearch(a, key: key, range: range.lowerBound ..< midIndex)
72-
73-
// Is the search key in the right half?
74-
} else if a[midIndex] < key {
75-
return binarySearch(a, key: key, range: midIndex + 1 ..< range.upperBound)
76-
77-
// If we get here, then we've found the search key!
78-
} else {
79-
return midIndex
80-
}
81-
}
61+
if range.lowerBound >= range.upperBound {
62+
// If we get here, then the search key is not present in the array.
63+
return nil
64+
65+
} else {
66+
// Calculate where to split the array.
67+
let midIndex = range.lowerBound + (range.upperBound - range.lowerBound) / 2
68+
69+
// Is the search key in the left half?
70+
if a[midIndex] > key {
71+
return binarySearch(a, key: key, range: range.lowerBound ..< midIndex)
72+
73+
// Is the search key in the right half?
74+
} else if a[midIndex] < key {
75+
return binarySearch(a, key: key, range: midIndex + 1 ..< range.upperBound)
76+
77+
// If we get here, then we've found the search key!
78+
} else {
79+
return midIndex
80+
}
81+
}
8282
}
8383
```
8484

@@ -123,11 +123,11 @@ Now binary search will determine which half to use. The relevant section from th
123123

124124
```swift
125125
if a[midIndex] > key {
126-
// use left half
126+
// use left half
127127
} else if a[midIndex] < key {
128-
// use right half
128+
// use right half
129129
} else {
130-
return midIndex
130+
return midIndex
131131
}
132132
```
133133

@@ -181,19 +181,19 @@ Here is an iterative implementation of binary search in Swift:
181181

182182
```swift
183183
func binarySearch<T: Comparable>(a: [T], key: T) -> Int? {
184-
var lowerBound = 0
185-
var upperBound = a.count
186-
while lowerBound < upperBound {
187-
let midIndex = lowerBound + (upperBound - lowerBound) / 2
188-
if a[midIndex] == key {
189-
return midIndex
190-
} else if a[midIndex] < key {
191-
lowerBound = midIndex + 1
192-
} else {
193-
upperBound = midIndex
194-
}
195-
}
196-
return nil
184+
var lowerBound = 0
185+
var upperBound = a.count
186+
while lowerBound < upperBound {
187+
let midIndex = lowerBound + (upperBound - lowerBound) / 2
188+
if a[midIndex] == key {
189+
return midIndex
190+
} else if a[midIndex] < key {
191+
lowerBound = midIndex + 1
192+
} else {
193+
upperBound = midIndex
194+
}
195+
}
196+
return nil
197197
}
198198
```
199199

0 commit comments

Comments
 (0)