{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Iterative Implementation of Binary Search\n", "def binSearchIterative(x,xs):\n", " left = 0\n", " right = len(xs) - 1\n", " while left <= right:\n", " mid = (left + right) // 2\n", " if xs[mid] < x:\n", " # x must be on the right side\n", " left = mid + 1\n", " elif xs[mid] == x:\n", " return True\n", " else:\n", " # x must be on the left side\n", " right = mid - 1\n", " return False" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "True\n", "False\n" ] } ], "source": [ "xs = [10,20,30,40,50,60,70,80,90,100,110,120,130,140,150]\n", "print(binSearchIterative(35,xs))\n", "print(binSearchIterative(50,xs))\n", "xs = []\n", "print(binSearchIterative(35,xs))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Iterative Implementation of Binary Search\n", "def binSearchIterative2(x,xs):\n", " num_iterations = 0\n", " while len(xs) > 0:\n", " num_iterations = num_iterations + 1\n", " mid = (0 + len(xs) - 1) // 2\n", " if xs[mid] < x:\n", " # x must be on the right side\n", " xs = xs[mid+1:]\n", " elif xs[mid] == x:\n", " return True\n", " else:\n", " # x must be on the left side\n", " xs = xs[0:mid-1]\n", " print(num_iterations)\n", " return False" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "False\n", "True\n", "0\n", "False\n" ] } ], "source": [ "xs = [10,20,30,40,50,60,70,80,90,100,110,120,130,140,150]\n", "print(binSearchIterative2(35,xs))\n", "print(binSearchIterative(50,xs))\n", "xs = []\n", "print(binSearchIterative2(35,xs))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "xs = []\n", "for i in range(1,1001):\n", " xs.append(10*i)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "False\n" ] } ], "source": [ "print(binSearchIterative2(35,xs))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Recursive Implementation of Binary Search\n", "def binSearchRecursive(x,xs):\n", " if len(xs) == 0:\n", " return False\n", " mid = (0 + len(xs) - 1) // 2\n", " if xs[mid] < x:\n", " # x must be on the right side\n", " return binSearchRecursive(x,xs[mid+1:])\n", " elif xs[mid] == x:\n", " return True\n", " else:\n", " # x must be on the left side\n", " return binSearchRecursive(x,xs[0:mid-1])" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "True\n", "False\n" ] } ], "source": [ "xs = [10,20,30,40,50,60,70,80,90,100,110,120,130,140,150]\n", "print(binSearchRecursive(35,xs))\n", "print(binSearchRecursive(50,xs))\n", "xs = []\n", "print(binSearchRecursive(35,xs))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "11//2" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def sequentialSearch(x,xs):\n", " for num in xs:\n", " if num == x:\n", " return True\n", " return False" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "True\n", "False\n" ] } ], "source": [ "xs = [10,20,30,40,50,60,70,80,90,100,110,120,130,140,150]\n", "print(sequentialSearch(35,xs))\n", "print(sequentialSearch(50,xs))\n", "xs = []\n", "print(sequentialSearch(35,xs))" ] } ], "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.12.1" } }, "nbformat": 4, "nbformat_minor": 2 }