Skip to content

Commit 0063f5f

Browse files
committed
recursive binary search
1 parent 9af565d commit 0063f5f

File tree

1 file changed

+153
-9
lines changed

1 file changed

+153
-9
lines changed

ipython_nbs/search/binary_search.ipynb

Lines changed: 153 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@
8383
{
8484
"cell_type": "code",
8585
"execution_count": 3,
86+
"metadata": {
87+
"collapsed": true
88+
},
89+
"outputs": [],
90+
"source": [
91+
"binary_search(array=[],\n",
92+
" value=1)"
93+
]
94+
},
95+
{
96+
"cell_type": "code",
97+
"execution_count": 4,
8698
"metadata": {},
8799
"outputs": [
88100
{
@@ -91,19 +103,19 @@
91103
"0"
92104
]
93105
},
94-
"execution_count": 3,
106+
"execution_count": 4,
95107
"metadata": {},
96108
"output_type": "execute_result"
97109
}
98110
],
99111
"source": [
100112
"binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n",
101-
" value=1)"
113+
" value=1)"
102114
]
103115
},
104116
{
105117
"cell_type": "code",
106-
"execution_count": 4,
118+
"execution_count": 5,
107119
"metadata": {},
108120
"outputs": [
109121
{
@@ -112,7 +124,7 @@
112124
"1"
113125
]
114126
},
115-
"execution_count": 4,
127+
"execution_count": 5,
116128
"metadata": {},
117129
"output_type": "execute_result"
118130
}
@@ -124,7 +136,7 @@
124136
},
125137
{
126138
"cell_type": "code",
127-
"execution_count": 5,
139+
"execution_count": 6,
128140
"metadata": {},
129141
"outputs": [
130142
{
@@ -133,7 +145,7 @@
133145
"2"
134146
]
135147
},
136-
"execution_count": 5,
148+
"execution_count": 6,
137149
"metadata": {},
138150
"output_type": "execute_result"
139151
}
@@ -145,7 +157,7 @@
145157
},
146158
{
147159
"cell_type": "code",
148-
"execution_count": 6,
160+
"execution_count": 7,
149161
"metadata": {},
150162
"outputs": [
151163
{
@@ -154,7 +166,7 @@
154166
"6"
155167
]
156168
},
157-
"execution_count": 6,
169+
"execution_count": 7,
158170
"metadata": {},
159171
"output_type": "execute_result"
160172
}
@@ -166,13 +178,145 @@
166178
},
167179
{
168180
"cell_type": "code",
169-
"execution_count": 7,
181+
"execution_count": 8,
170182
"metadata": {},
171183
"outputs": [],
172184
"source": [
173185
"binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n",
174186
" value=99)"
175187
]
188+
},
189+
{
190+
"cell_type": "markdown",
191+
"metadata": {},
192+
"source": [
193+
"## Binary Search using Recursion"
194+
]
195+
},
196+
{
197+
"cell_type": "markdown",
198+
"metadata": {},
199+
"source": [
200+
"Note that this implementation of recursive binary search deliberately avoid slicing the `array` (e.g., `array[:middle_idx]`), because slicing Python lists is expensive due to the random memory access. E.g., slicing a Python list with as `a_list[:k]` is an O(k) operation."
201+
]
202+
},
203+
{
204+
"cell_type": "code",
205+
"execution_count": 9,
206+
"metadata": {},
207+
"outputs": [],
208+
"source": [
209+
"def recursive_binary_search(array, value, start_idx=None, end_idx=None):\n",
210+
" \n",
211+
" len_ary = len(array)\n",
212+
" \n",
213+
" if start_idx is None:\n",
214+
" start_idx = 0\n",
215+
" if end_idx is None:\n",
216+
" end_idx = len(array) - 1\n",
217+
" \n",
218+
" if not len_ary or start_idx >= end_idx:\n",
219+
" return None\n",
220+
" \n",
221+
" middle_idx = (start_idx + end_idx) // 2\n",
222+
" if array[middle_idx] == value:\n",
223+
" return middle_idx\n",
224+
"\n",
225+
" elif array[middle_idx] > value:\n",
226+
" return recursive_binary_search(array, \n",
227+
" value, \n",
228+
" start_idx=start_idx,\n",
229+
" end_idx=middle_idx)\n",
230+
" else:\n",
231+
" return recursive_binary_search(array,\n",
232+
" value,\n",
233+
" start_idx=middle_idx + 1,\n",
234+
" end_idx=len_ary)\n",
235+
" return None"
236+
]
237+
},
238+
{
239+
"cell_type": "code",
240+
"execution_count": 10,
241+
"metadata": {},
242+
"outputs": [],
243+
"source": [
244+
"recursive_binary_search(array=[],\n",
245+
" value=1)"
246+
]
247+
},
248+
{
249+
"cell_type": "code",
250+
"execution_count": 11,
251+
"metadata": {},
252+
"outputs": [
253+
{
254+
"data": {
255+
"text/plain": [
256+
"0"
257+
]
258+
},
259+
"execution_count": 11,
260+
"metadata": {},
261+
"output_type": "execute_result"
262+
}
263+
],
264+
"source": [
265+
"recursive_binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n",
266+
" value=1)"
267+
]
268+
},
269+
{
270+
"cell_type": "code",
271+
"execution_count": 12,
272+
"metadata": {},
273+
"outputs": [
274+
{
275+
"data": {
276+
"text/plain": [
277+
"2"
278+
]
279+
},
280+
"execution_count": 12,
281+
"metadata": {},
282+
"output_type": "execute_result"
283+
}
284+
],
285+
"source": [
286+
"recursive_binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n",
287+
" value=4)"
288+
]
289+
},
290+
{
291+
"cell_type": "code",
292+
"execution_count": 13,
293+
"metadata": {},
294+
"outputs": [
295+
{
296+
"data": {
297+
"text/plain": [
298+
"6"
299+
]
300+
},
301+
"execution_count": 13,
302+
"metadata": {},
303+
"output_type": "execute_result"
304+
}
305+
],
306+
"source": [
307+
"recursive_binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n",
308+
" value=11)"
309+
]
310+
},
311+
{
312+
"cell_type": "code",
313+
"execution_count": 14,
314+
"metadata": {},
315+
"outputs": [],
316+
"source": [
317+
"recursive_binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n",
318+
" value=99)"
319+
]
176320
}
177321
],
178322
"metadata": {

0 commit comments

Comments
 (0)