@@ -16,12 +16,12 @@ The built-in `indexOf()` function performs a [linear search](../Linear Search/).
16
16
17
17
``` swift
18
18
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
25
25
}
26
26
```
27
27
@@ -58,27 +58,27 @@ Here is a recursive implementation of binary search in Swift:
58
58
59
59
``` swift
60
60
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
+ }
82
82
}
83
83
```
84
84
@@ -123,11 +123,11 @@ Now binary search will determine which half to use. The relevant section from th
123
123
124
124
``` swift
125
125
if a[midIndex] > key {
126
- // use left half
126
+ // use left half
127
127
} else if a[midIndex] < key {
128
- // use right half
128
+ // use right half
129
129
} else {
130
- return midIndex
130
+ return midIndex
131
131
}
132
132
```
133
133
@@ -181,19 +181,19 @@ Here is an iterative implementation of binary search in Swift:
181
181
182
182
``` swift
183
183
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
197
197
}
198
198
```
199
199
0 commit comments