|
83 | 83 | { |
84 | 84 | "cell_type": "code", |
85 | 85 | "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, |
86 | 98 | "metadata": {}, |
87 | 99 | "outputs": [ |
88 | 100 | { |
|
91 | 103 | "0" |
92 | 104 | ] |
93 | 105 | }, |
94 | | - "execution_count": 3, |
| 106 | + "execution_count": 4, |
95 | 107 | "metadata": {}, |
96 | 108 | "output_type": "execute_result" |
97 | 109 | } |
98 | 110 | ], |
99 | 111 | "source": [ |
100 | 112 | "binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n", |
101 | | - " value=1)" |
| 113 | + " value=1)" |
102 | 114 | ] |
103 | 115 | }, |
104 | 116 | { |
105 | 117 | "cell_type": "code", |
106 | | - "execution_count": 4, |
| 118 | + "execution_count": 5, |
107 | 119 | "metadata": {}, |
108 | 120 | "outputs": [ |
109 | 121 | { |
|
112 | 124 | "1" |
113 | 125 | ] |
114 | 126 | }, |
115 | | - "execution_count": 4, |
| 127 | + "execution_count": 5, |
116 | 128 | "metadata": {}, |
117 | 129 | "output_type": "execute_result" |
118 | 130 | } |
|
124 | 136 | }, |
125 | 137 | { |
126 | 138 | "cell_type": "code", |
127 | | - "execution_count": 5, |
| 139 | + "execution_count": 6, |
128 | 140 | "metadata": {}, |
129 | 141 | "outputs": [ |
130 | 142 | { |
|
133 | 145 | "2" |
134 | 146 | ] |
135 | 147 | }, |
136 | | - "execution_count": 5, |
| 148 | + "execution_count": 6, |
137 | 149 | "metadata": {}, |
138 | 150 | "output_type": "execute_result" |
139 | 151 | } |
|
145 | 157 | }, |
146 | 158 | { |
147 | 159 | "cell_type": "code", |
148 | | - "execution_count": 6, |
| 160 | + "execution_count": 7, |
149 | 161 | "metadata": {}, |
150 | 162 | "outputs": [ |
151 | 163 | { |
|
154 | 166 | "6" |
155 | 167 | ] |
156 | 168 | }, |
157 | | - "execution_count": 6, |
| 169 | + "execution_count": 7, |
158 | 170 | "metadata": {}, |
159 | 171 | "output_type": "execute_result" |
160 | 172 | } |
|
166 | 178 | }, |
167 | 179 | { |
168 | 180 | "cell_type": "code", |
169 | | - "execution_count": 7, |
| 181 | + "execution_count": 8, |
170 | 182 | "metadata": {}, |
171 | 183 | "outputs": [], |
172 | 184 | "source": [ |
173 | 185 | "binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n", |
174 | 186 | " value=99)" |
175 | 187 | ] |
| 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 | + ] |
176 | 320 | } |
177 | 321 | ], |
178 | 322 | "metadata": { |
|
0 commit comments