Skip to content

Commit 2123c70

Browse files
committed
recursive binary search
1 parent b6490f8 commit 2123c70

File tree

1 file changed

+198
-8
lines changed

1 file changed

+198
-8
lines changed

ipython_nbs/essentials/recursion-examples.ipynb

Lines changed: 198 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,27 +131,87 @@
131131
"cell_type": "markdown",
132132
"metadata": {},
133133
"source": [
134-
"## Array Sum"
134+
"## Length of an array"
135135
]
136136
},
137137
{
138138
"cell_type": "code",
139139
"execution_count": 6,
140+
"metadata": {},
141+
"outputs": [],
142+
"source": [
143+
"def array_len(x):\n",
144+
" if x == []:\n",
145+
" return 0\n",
146+
" else:\n",
147+
" return 1 + array_len(x[1:])"
148+
]
149+
},
150+
{
151+
"cell_type": "code",
152+
"execution_count": 7,
153+
"metadata": {},
154+
"outputs": [
155+
{
156+
"data": {
157+
"text/plain": [
158+
"0"
159+
]
160+
},
161+
"execution_count": 7,
162+
"metadata": {},
163+
"output_type": "execute_result"
164+
}
165+
],
166+
"source": [
167+
"array_len([])"
168+
]
169+
},
170+
{
171+
"cell_type": "code",
172+
"execution_count": 8,
173+
"metadata": {},
174+
"outputs": [
175+
{
176+
"data": {
177+
"text/plain": [
178+
"3"
179+
]
180+
},
181+
"execution_count": 8,
182+
"metadata": {},
183+
"output_type": "execute_result"
184+
}
185+
],
186+
"source": [
187+
"array_len([1, 2, 3])"
188+
]
189+
},
190+
{
191+
"cell_type": "markdown",
192+
"metadata": {},
193+
"source": [
194+
"## Sum of the elements in an array"
195+
]
196+
},
197+
{
198+
"cell_type": "code",
199+
"execution_count": 9,
140200
"metadata": {
141201
"collapsed": true
142202
},
143203
"outputs": [],
144204
"source": [
145205
"def array_sum(x):\n",
146-
" if not len(x):\n",
206+
" if x == []:\n",
147207
" return 0\n",
148208
" else:\n",
149209
" return x[0] + array_sum(x[1:])"
150210
]
151211
},
152212
{
153213
"cell_type": "code",
154-
"execution_count": 7,
214+
"execution_count": 10,
155215
"metadata": {},
156216
"outputs": [
157217
{
@@ -160,7 +220,7 @@
160220
"0"
161221
]
162222
},
163-
"execution_count": 7,
223+
"execution_count": 10,
164224
"metadata": {},
165225
"output_type": "execute_result"
166226
}
@@ -171,7 +231,7 @@
171231
},
172232
{
173233
"cell_type": "code",
174-
"execution_count": 8,
234+
"execution_count": 11,
175235
"metadata": {},
176236
"outputs": [
177237
{
@@ -180,7 +240,7 @@
180240
"5"
181241
]
182242
},
183-
"execution_count": 8,
243+
"execution_count": 11,
184244
"metadata": {},
185245
"output_type": "execute_result"
186246
}
@@ -191,7 +251,7 @@
191251
},
192252
{
193253
"cell_type": "code",
194-
"execution_count": 9,
254+
"execution_count": 12,
195255
"metadata": {},
196256
"outputs": [
197257
{
@@ -200,14 +260,144 @@
200260
"15"
201261
]
202262
},
203-
"execution_count": 9,
263+
"execution_count": 12,
204264
"metadata": {},
205265
"output_type": "execute_result"
206266
}
207267
],
208268
"source": [
209269
"array_sum([1, 2, 3, 4, 5])"
210270
]
271+
},
272+
{
273+
"cell_type": "markdown",
274+
"metadata": {},
275+
"source": [
276+
"## Binary search using recursion"
277+
]
278+
},
279+
{
280+
"cell_type": "code",
281+
"execution_count": 13,
282+
"metadata": {
283+
"collapsed": true
284+
},
285+
"outputs": [],
286+
"source": [
287+
"def binary_search(array, value, min_idx=0, max_idx=None):\n",
288+
" \n",
289+
" if min_idx == max_idx:\n",
290+
" return None\n",
291+
" elif max_idx is None:\n",
292+
" max_idx = len(array)\n",
293+
" else:\n",
294+
" pass\n",
295+
"\n",
296+
" middle_idx = min_idx + (max_idx - min_idx) // 2\n",
297+
"\n",
298+
" if array[middle_idx] == value:\n",
299+
" return middle_idx\n",
300+
" elif array[middle_idx] < value:\n",
301+
" min_idx = middle_idx + 1\n",
302+
" else:\n",
303+
" max_idx = middle_idx\n",
304+
" \n",
305+
" return binary_search(array, value, min_idx, max_idx)"
306+
]
307+
},
308+
{
309+
"cell_type": "code",
310+
"execution_count": 14,
311+
"metadata": {},
312+
"outputs": [
313+
{
314+
"data": {
315+
"text/plain": [
316+
"0"
317+
]
318+
},
319+
"execution_count": 14,
320+
"metadata": {},
321+
"output_type": "execute_result"
322+
}
323+
],
324+
"source": [
325+
"binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n",
326+
" value=1)"
327+
]
328+
},
329+
{
330+
"cell_type": "code",
331+
"execution_count": 15,
332+
"metadata": {},
333+
"outputs": [
334+
{
335+
"data": {
336+
"text/plain": [
337+
"1"
338+
]
339+
},
340+
"execution_count": 15,
341+
"metadata": {},
342+
"output_type": "execute_result"
343+
}
344+
],
345+
"source": [
346+
"binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n",
347+
" value=2)"
348+
]
349+
},
350+
{
351+
"cell_type": "code",
352+
"execution_count": 16,
353+
"metadata": {},
354+
"outputs": [
355+
{
356+
"data": {
357+
"text/plain": [
358+
"2"
359+
]
360+
},
361+
"execution_count": 16,
362+
"metadata": {},
363+
"output_type": "execute_result"
364+
}
365+
],
366+
"source": [
367+
"binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n",
368+
" value=4)"
369+
]
370+
},
371+
{
372+
"cell_type": "code",
373+
"execution_count": 17,
374+
"metadata": {},
375+
"outputs": [
376+
{
377+
"data": {
378+
"text/plain": [
379+
"6"
380+
]
381+
},
382+
"execution_count": 17,
383+
"metadata": {},
384+
"output_type": "execute_result"
385+
}
386+
],
387+
"source": [
388+
"binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n",
389+
" value=11)"
390+
]
391+
},
392+
{
393+
"cell_type": "code",
394+
"execution_count": 18,
395+
"metadata": {},
396+
"outputs": [],
397+
"source": [
398+
"binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n",
399+
" value=99)"
400+
]
211401
}
212402
],
213403
"metadata": {

0 commit comments

Comments
 (0)