|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [ |
| 8 | + { |
| 9 | + "name": "stdout", |
| 10 | + "output_type": "stream", |
| 11 | + "text": [ |
| 12 | + "Sebastian Raschka \n", |
| 13 | + "last updated: 2017-07-25 \n", |
| 14 | + "\n", |
| 15 | + "CPython 3.6.1\n", |
| 16 | + "IPython 6.0.0\n" |
| 17 | + ] |
| 18 | + } |
| 19 | + ], |
| 20 | + "source": [ |
| 21 | + "%load_ext watermark\n", |
| 22 | + "%watermark -a 'Sebastian Raschka' -u -d -v" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "markdown", |
| 27 | + "metadata": {}, |
| 28 | + "source": [ |
| 29 | + "# Binary Search" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "markdown", |
| 34 | + "metadata": {}, |
| 35 | + "source": [ |
| 36 | + "An implementation of the binary search algorithm. For details will follow. A good summary can be found on Wikipedia: https://en.wikipedia.org/wiki/Binary_search_algorithm." |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "markdown", |
| 41 | + "metadata": {}, |
| 42 | + "source": [ |
| 43 | + "The figures below provide a short illustration of how the implementation works on a toy example:\n", |
| 44 | + "\n", |
| 45 | + "\n", |
| 46 | + "\n", |
| 47 | + "" |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "code", |
| 52 | + "execution_count": 2, |
| 53 | + "metadata": { |
| 54 | + "collapsed": true |
| 55 | + }, |
| 56 | + "outputs": [], |
| 57 | + "source": [ |
| 58 | + "def binary_search(array, value):\n", |
| 59 | + " ary = array\n", |
| 60 | + " min_idx = 0\n", |
| 61 | + " max_idx = len(array)\n", |
| 62 | + " \n", |
| 63 | + " while min_idx < max_idx:\n", |
| 64 | + " middle_idx = min_idx + (max_idx - min_idx) // 2\n", |
| 65 | + "\n", |
| 66 | + " if array[middle_idx] == value:\n", |
| 67 | + " return middle_idx\n", |
| 68 | + " elif array[middle_idx] < value:\n", |
| 69 | + " min_idx = middle_idx + 1\n", |
| 70 | + " else:\n", |
| 71 | + " max_idx = middle_idx\n", |
| 72 | + " \n", |
| 73 | + " return None" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "code", |
| 78 | + "execution_count": 3, |
| 79 | + "metadata": {}, |
| 80 | + "outputs": [ |
| 81 | + { |
| 82 | + "data": { |
| 83 | + "text/plain": [ |
| 84 | + "0" |
| 85 | + ] |
| 86 | + }, |
| 87 | + "execution_count": 3, |
| 88 | + "metadata": {}, |
| 89 | + "output_type": "execute_result" |
| 90 | + } |
| 91 | + ], |
| 92 | + "source": [ |
| 93 | + "binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n", |
| 94 | + " value=1)" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": 4, |
| 100 | + "metadata": {}, |
| 101 | + "outputs": [ |
| 102 | + { |
| 103 | + "data": { |
| 104 | + "text/plain": [ |
| 105 | + "1" |
| 106 | + ] |
| 107 | + }, |
| 108 | + "execution_count": 4, |
| 109 | + "metadata": {}, |
| 110 | + "output_type": "execute_result" |
| 111 | + } |
| 112 | + ], |
| 113 | + "source": [ |
| 114 | + "binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n", |
| 115 | + " value=2)" |
| 116 | + ] |
| 117 | + }, |
| 118 | + { |
| 119 | + "cell_type": "code", |
| 120 | + "execution_count": 5, |
| 121 | + "metadata": {}, |
| 122 | + "outputs": [ |
| 123 | + { |
| 124 | + "data": { |
| 125 | + "text/plain": [ |
| 126 | + "2" |
| 127 | + ] |
| 128 | + }, |
| 129 | + "execution_count": 5, |
| 130 | + "metadata": {}, |
| 131 | + "output_type": "execute_result" |
| 132 | + } |
| 133 | + ], |
| 134 | + "source": [ |
| 135 | + "binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n", |
| 136 | + " value=4)" |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "code", |
| 141 | + "execution_count": 6, |
| 142 | + "metadata": {}, |
| 143 | + "outputs": [ |
| 144 | + { |
| 145 | + "data": { |
| 146 | + "text/plain": [ |
| 147 | + "6" |
| 148 | + ] |
| 149 | + }, |
| 150 | + "execution_count": 6, |
| 151 | + "metadata": {}, |
| 152 | + "output_type": "execute_result" |
| 153 | + } |
| 154 | + ], |
| 155 | + "source": [ |
| 156 | + "binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n", |
| 157 | + " value=11)" |
| 158 | + ] |
| 159 | + }, |
| 160 | + { |
| 161 | + "cell_type": "code", |
| 162 | + "execution_count": 7, |
| 163 | + "metadata": { |
| 164 | + "collapsed": true |
| 165 | + }, |
| 166 | + "outputs": [], |
| 167 | + "source": [ |
| 168 | + "binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n", |
| 169 | + " value=99)" |
| 170 | + ] |
| 171 | + } |
| 172 | + ], |
| 173 | + "metadata": { |
| 174 | + "kernelspec": { |
| 175 | + "display_name": "Python 3", |
| 176 | + "language": "python", |
| 177 | + "name": "python3" |
| 178 | + }, |
| 179 | + "language_info": { |
| 180 | + "codemirror_mode": { |
| 181 | + "name": "ipython", |
| 182 | + "version": 3 |
| 183 | + }, |
| 184 | + "file_extension": ".py", |
| 185 | + "mimetype": "text/x-python", |
| 186 | + "name": "python", |
| 187 | + "nbconvert_exporter": "python", |
| 188 | + "pygments_lexer": "ipython3", |
| 189 | + "version": "3.6.1" |
| 190 | + } |
| 191 | + }, |
| 192 | + "nbformat": 4, |
| 193 | + "nbformat_minor": 2 |
| 194 | +} |
0 commit comments