Skip to content

Commit

Permalink
recursive binary search
Browse files Browse the repository at this point in the history
  • Loading branch information
rasbt committed Aug 14, 2017
1 parent 9af565d commit 0063f5f
Showing 1 changed file with 153 additions and 9 deletions.
162 changes: 153 additions & 9 deletions ipython_nbs/search/binary_search.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"binary_search(array=[],\n",
" value=1)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
Expand All @@ -91,19 +103,19 @@
"0"
]
},
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n",
" value=1)"
" value=1)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand All @@ -112,7 +124,7 @@
"1"
]
},
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -124,7 +136,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand All @@ -133,7 +145,7 @@
"2"
]
},
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -145,7 +157,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand All @@ -154,7 +166,7 @@
"6"
]
},
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -166,13 +178,145 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n",
" value=99)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Binary Search using Recursion"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"def recursive_binary_search(array, value, start_idx=None, end_idx=None):\n",
" \n",
" len_ary = len(array)\n",
" \n",
" if start_idx is None:\n",
" start_idx = 0\n",
" if end_idx is None:\n",
" end_idx = len(array) - 1\n",
" \n",
" if not len_ary or start_idx >= end_idx:\n",
" return None\n",
" \n",
" middle_idx = (start_idx + end_idx) // 2\n",
" if array[middle_idx] == value:\n",
" return middle_idx\n",
"\n",
" elif array[middle_idx] > value:\n",
" return recursive_binary_search(array, \n",
" value, \n",
" start_idx=start_idx,\n",
" end_idx=middle_idx)\n",
" else:\n",
" return recursive_binary_search(array,\n",
" value,\n",
" start_idx=middle_idx + 1,\n",
" end_idx=len_ary)\n",
" return None"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"recursive_binary_search(array=[],\n",
" value=1)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"recursive_binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n",
" value=1)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"recursive_binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n",
" value=4)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"recursive_binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n",
" value=11)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"recursive_binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n",
" value=99)"
]
}
],
"metadata": {
Expand Down

0 comments on commit 0063f5f

Please sign in to comment.