{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sebastian Raschka \n", "last updated: 2017-08-14 \n", "\n", "CPython 3.6.1\n", "IPython 6.1.0\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -a 'Sebastian Raschka' -u -d -v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Binary Search" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The figures below provide a short illustration of how the implementation works on a toy example:\n", "\n", "![](images/binary_search/ex-1-1.png)\n", "\n", "![](images/binary_search/ex-1-2.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Binary Search Implementation" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def binary_search(array, value):\n", " ary = array\n", " min_idx = 0\n", " max_idx = len(array)\n", " \n", " while min_idx < max_idx:\n", " middle_idx = (min_idx + max_idx) // 2\n", "\n", " if array[middle_idx] == value:\n", " return middle_idx\n", " elif array[middle_idx] < value:\n", " min_idx = middle_idx + 1\n", " else:\n", " max_idx = middle_idx\n", " \n", " return None" ] }, { "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": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n", " value=1)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n", " value=2)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n", " value=4)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n", " value=11)" ] }, { "cell_type": "code", "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": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }