Skip to content

Commit 232c92f

Browse files
committed
Fix array midpoint figures in Binary Search readme
1 parent fba86e1 commit 232c92f

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

Binary Search/README.markdown

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ It might be useful to look at how the algorithm works in detail.
102102

103103
The array from the above example consists of 19 numbers and looks like this when sorted:
104104

105-
[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67 ]
105+
[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67 ]
106106

107107
We're trying to determine if the number `43` is in this array.
108108

@@ -116,8 +116,8 @@ Initially, the range has `lowerBound = 0` and `upperBound = 19`. Filling in thes
116116

117117
In the next figure, the `*` shows the middle item. As you can see, the number of items on each side is the same, so we're split right down the middle.
118118

119-
[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67 ]
120-
*
119+
[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67 ]
120+
*
121121

122122
Now binary search will determine which half to use. The relevant section from the code is:
123123

@@ -135,35 +135,35 @@ In this case, `a[midIndex] = 29`. That's less than the search key, so we can saf
135135

136136
Now we can simply repeat the binary search, but on the array interval from `midIndex + 1` to `range.upperBound`:
137137

138-
[ x, x, x, x, x, x, x, x, x, x | 31, 37, 41, 43, 47, 53, 59, 61, 67 ]
138+
[ x, x, x, x, x, x, x, x, x, x | 31, 37, 41, 43, 47, 53, 59, 61, 67 ]
139139

140140
Since we no longer need to concern ourselves with the left half of the array, I've marked that with `x`'s. From now on we'll only look at the right half, which starts at array index 10.
141141

142142
We calculate the index of the new middle element: `midIndex = 10 + (19 - 10)/2 = 14`, and split the array down the middle again.
143143

144-
[ x, x, x, x, x, x, x, x, x, x | 31, 37, 41, 43, 47, 53, 59, 61, 67 ]
145-
*
144+
[ x, x, x, x, x, x, x, x, x, x | 31, 37, 41, 43, 47, 53, 59, 61, 67 ]
145+
*
146146

147147
As you can see, `a[14]` is indeed the middle element of the array's right half.
148148

149149
Is the search key greater or smaller than `a[14]`? It's smaller because `43 < 47`. This time we're taking the left half and ignore the larger numbers on the right:
150150

151-
[ x, x, x, x, x, x, x, x, x, x | 31, 37, 41, 43 | x, x, x, x, x ]
151+
[ x, x, x, x, x, x, x, x, x, x | 31, 37, 41, 43 | x, x, x, x, x ]
152152

153153
The new `midIndex` is here:
154154

155-
[ x, x, x, x, x, x, x, x, x, x | 31, 37, 41, 43 | x, x, x, x, x ]
156-
*
155+
[ x, x, x, x, x, x, x, x, x, x | 31, 37, 41, 43 | x, x, x, x, x ]
156+
*
157157

158158
The search key is greater than `37`, so continue with the right side:
159159

160-
[ x, x, x, x, x, x, x, x, x, x | x, x | 41, 43 | x, x, x, x, x ]
161-
*
160+
[ x, x, x, x, x, x, x, x, x, x | x, x | 41, 43 | x, x, x, x, x ]
161+
*
162162

163163
Again, the search key is greater, so split once more and take the right side:
164164

165-
[ x, x, x, x, x, x, x, x, x, x | x, x | x | 43 | x, x, x, x, x ]
166-
*
165+
[ x, x, x, x, x, x, x, x, x, x | x, x | x | 43 | x, x, x, x, x ]
166+
*
167167

168168
And now we're done. The search key equals the array element we're looking at, so we've finally found what we were searching for: number `43` is at array index `13`. w00t!
169169

0 commit comments

Comments
 (0)