{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "bvzwd5SvsZ9M"
   },
   "source": [
    "# PDSP2025, Lecture 22, 4 November 2025\n",
    "## Searching and Sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "HafrRO7osZ9N"
   },
   "source": [
    "### Setup\n",
    "- Use `time` library to time executions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "id": "FX-LCujtXSyH"
   },
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "HREqZFVosZ9R"
   },
   "source": [
    "### Naive search by scanning the list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "id": "Nh0Rt2ztm4nL"
   },
   "outputs": [],
   "source": [
    "def naivesearch(v,l):\n",
    "  for x in l:\n",
    "    if v == x:\n",
    "      return(True)\n",
    "  return(False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "_I_Kwq5RsZ9S"
   },
   "source": [
    "### Binary search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "id": "9tDWAhyUnA7K"
   },
   "outputs": [],
   "source": [
    "def binarysearch(v,l):\n",
    "  if l == []:\n",
    "    return(False)\n",
    "\n",
    "  m = len(l)//2\n",
    "\n",
    "  if v == l[m]:\n",
    "    return(True)\n",
    "\n",
    "  if v < l[m]:\n",
    "    return(binarysearch(v,l[:m]))\n",
    "  else:\n",
    "    return(binarysearch(v,l[m+1:]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "4Psd7PytsZ9U"
   },
   "source": [
    "### Checking correctness on input `[0,2,...,50]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 0
    },
    "id": "Z20emTJ9sZ9V",
    "outputId": "46c4e5de-1ffb-44ce-c81f-da8228c5846c"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, True),(1, False),(2, True),(3, False),(4, True),(5, False),(6, True),(7, False),(8, True),(9, False),(10, True),(11, False),(12, True),(13, False),(14, True),(15, False),(16, True),(17, False),(18, True),(19, False),(20, True),(21, False),(22, True),(23, False),(24, True),(25, False),(26, True),(27, False),(28, True),(29, False),(30, True),(31, False),(32, True),(33, False),(34, True),(35, False),(36, True),(37, False),(38, True),(39, False),(40, True),(41, False),(42, True),(43, False),(44, True),(45, False),(46, True),(47, False),(48, True),(49, False),(50, True),\n",
      "(0, True),(1, False),(2, True),(3, False),(4, True),(5, False),(6, True),(7, False),(8, True),(9, False),(10, True),(11, False),(12, True),(13, False),(14, True),(15, False),(16, True),(17, False),(18, True),(19, False),(20, True),(21, False),(22, True),(23, False),(24, True),(25, False),(26, True),(27, False),(28, True),(29, False),(30, True),(31, False),(32, True),(33, False),(34, True),(35, False),(36, True),(37, False),(38, True),(39, False),(40, True),(41, False),(42, True),(43, False),(44, True),(45, False),(46, True),(47, False),(48, True),(49, False),(50, True),\n"
     ]
    }
   ],
   "source": [
    "l = list(range(0,51,2))\n",
    "\n",
    "for i in range(51):\n",
    "  print((i,naivesearch(i,l)),end=\",\")\n",
    "print()\n",
    "\n",
    "for i in range(51):\n",
    "  print((i,binarysearch(i,l)),end=\",\")\n",
    "print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "FcsOBFTGsZ9V"
   },
   "source": [
    "### Performance comparison across $10^4$ worst case searches in a list of size $10^5$\n",
    "- Looking for odd numbers in a list of even numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 0
    },
    "id": "BlsFK6JdoC6L",
    "outputId": "99a83989-033d-4087-b16a-392c556e260a"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Naive search 17.956641599004797\n",
      "\n",
      "Binary search 1.9248965570004657\n"
     ]
    }
   ],
   "source": [
    "l = list(range(0,200000,2))\n",
    "\n",
    "starttime = time.perf_counter()\n",
    "for i in range(3001,23000,2):\n",
    "  v = naivesearch(i,l)\n",
    "elapsed = time.perf_counter() - starttime\n",
    "print()\n",
    "print(\"Naive search\", elapsed)\n",
    "\n",
    "starttime = time.perf_counter()\n",
    "for i in range(3001,23000,2):\n",
    "  v = binarysearch(i,l)\n",
    "elapsed = time.perf_counter() - starttime\n",
    "print()\n",
    "print(\"Binary search\", elapsed)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "IrkFYqxMsZ9W"
   },
   "source": [
    "### Selection sort"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "id": "Xv8jrh12ouDC"
   },
   "outputs": [],
   "source": [
    "def SelectionSort(L):\n",
    "   n = len(L)\n",
    "   if n < 1:\n",
    "      return(L)\n",
    "   for i in range(n):\n",
    "      # Assume L[:i] is sorted\n",
    "      mpos = i  \n",
    "      # mpos is position of minimum in L[i:]\n",
    "      for j in range(i+1,n):\n",
    "        if L[j] < L[mpos]:\n",
    "           mpos = j\n",
    "      # L[mpos] is the smallest value in L[i:]\n",
    "      (L[i],L[mpos]) = (L[mpos],L[i])\n",
    "      # Now L[:i+1] is sorted\n",
    "   return(L)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "FAUrIDbtsZ9X"
   },
   "source": [
    "### Selection sort performance is more or less the same for all inputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 0
    },
    "id": "FlOt-RwxoUvX",
    "outputId": "277b0cf2-ab6d-44ac-b81f-2822bf67a81b"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "random 0.41388963800272904\n",
      "ascending 0.3815801900054794\n",
      "descending 0.390925444997265\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "random.seed(2025)\n",
    "inputlists = {}\n",
    "inputlists[\"random\"] = [random.randrange(100000) for i in range(5000)]\n",
    "inputlists[\"ascending\"] = [i for i in range(5000)]\n",
    "inputlists[\"descending\"] = [i for i in range (4999,-1,-1)]\n",
    "for k in inputlists.keys():\n",
    "    tmplist = inputlists[k][:]\n",
    "    starttime = time.perf_counter()\n",
    "    SelectionSort(tmplist)\n",
    "    elapsed = time.perf_counter() - starttime\n",
    "    print(k,elapsed)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "LZYEt3CMsZ9Y"
   },
   "source": [
    "### Insertion sort, iterative"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "id": "7IU966hV-9Pn"
   },
   "outputs": [],
   "source": [
    "def InsertionSort(L):\n",
    "   n = len(L)\n",
    "   if n < 1:\n",
    "      return(L)\n",
    "   for i in range(n):\n",
    "      # Assume L[:i] is sorted\n",
    "      # Move L[i] to correct position in L[:i]\n",
    "      j = i\n",
    "      while(j > 0 and L[j] < L[j-1]):\n",
    "        (L[j],L[j-1]) = (L[j-1],L[j])\n",
    "        j = j-1\n",
    "      # Now L[:i+1] is sorted\n",
    "   return(L)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "BYq-qcbTsZ9Y"
   },
   "source": [
    "### Insertion sort preformance\n",
    "- On already sorted input, performance is very good\n",
    "- On reverse sorted input, performance is worse than selection sort"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 0
    },
    "id": "P59KcaKM_CjK",
    "outputId": "e4c27e71-7efa-4415-e424-a81bb2a2988d"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "random 0.5835713570049847\n",
      "ascending 0.0002267620002385229\n",
      "descending 1.1273996609961614\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "random.seed(2025)\n",
    "inputlists = {}\n",
    "inputlists[\"random\"] = [random.randrange(100000) for i in range(5000)]\n",
    "inputlists[\"ascending\"] = [i for i in range(5000)]\n",
    "inputlists[\"descending\"] = [i for i in range (4999,-1,-1)]\n",
    "for k in inputlists.keys():\n",
    "    tmplist = inputlists[k][:]\n",
    "    starttime = time.perf_counter()\n",
    "    InsertionSort(tmplist)\n",
    "    elapsed = time.perf_counter() - starttime\n",
    "    print(k,elapsed)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "YgUP1eBLsZ9Z"
   },
   "source": [
    "### Insertion sort, recursive"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "UaZ2ymUs3Ioq"
   },
   "source": [
    "### Setup\n",
    "- Set recursion limit to maxint,  $2^{31}-1$\n",
    "    - This is the highest value Python allows\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "id": "W7aahHWJ3Col"
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.setrecursionlimit(2**31-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "id": "cknMO2qA_ZFX"
   },
   "outputs": [],
   "source": [
    "def Insert(L,v):\n",
    "   n = len(L)\n",
    "   if n == 0:\n",
    "     return([v])\n",
    "   if v >= L[-1]:\n",
    "     return(L+[v])\n",
    "   else:\n",
    "     return(Insert(L[:-1],v)+L[-1:])\n",
    "\n",
    "def ISort(L):\n",
    "   n = len(L)\n",
    "   if n < 1:\n",
    "      return(L)\n",
    "   L = Insert(ISort(L[:-1]),L[-1])\n",
    "   return(L)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "mOV6kW0ZsZ9a"
   },
   "source": [
    "### Recursive insertion sort is slower than iterative\n",
    "- Input of 2000 (40%) takes more time than 5000 for iterative\n",
    "  - Overhead of recursive calls\n",
    "- Performance pattern between unsorted, sorted and random is similar "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 0
    },
    "id": "jtSsgZhV_ke1",
    "outputId": "3fcce238-b97b-4044-9661-5143e543fb2a"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "random 2.9386179680004716\n",
      "ascending 0.006556373999046627\n",
      "descending 4.456001231003029\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "random.seed(2025)\n",
    "\n",
    "inputlists = {}\n",
    "inputlists[\"random\"] = [random.randrange(100000) for i in range(2000)]\n",
    "inputlists[\"ascending\"] = [i for i in range(2000)]\n",
    "inputlists[\"descending\"] = [i for i in range (1999,-1,-1)]\n",
    "for k in inputlists.keys():\n",
    "    tmplist = inputlists[k][:]\n",
    "    starttime = time.perf_counter()\n",
    "    ISort(tmplist)\n",
    "    elapsed = time.perf_counter() - starttime\n",
    "    print(k,elapsed)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ugj-kRXpsZ9b"
   },
   "source": [
    "### Merge sort"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "id": "vjSICLy_s0-K"
   },
   "outputs": [],
   "source": [
    "def merge(A,B):\n",
    "  (m,n) = (len(A),len(B))\n",
    "  (C,i,j,k) = ([],0,0,0)\n",
    "  while k < m+n:\n",
    "    if i == m:\n",
    "      C.extend(B[j:])\n",
    "      k = k + (n-j)\n",
    "    elif j == n:\n",
    "      C.extend(A[i:])\n",
    "      k = k + (n-i)\n",
    "    elif A[i] < B[j]:\n",
    "      C.append(A[i])\n",
    "      (i,k) = (i+1,k+1)\n",
    "    else:\n",
    "      C.append(B[j])\n",
    "      (j,k) = (j+1,k+1)\n",
    "  return(C)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "id": "YAAEaiRxs6Yu"
   },
   "outputs": [],
   "source": [
    "def mergesort(A):\n",
    "  n = len(A)\n",
    "\n",
    "  if n <= 1:\n",
    "     return(A)\n",
    "  \n",
    "  L = mergesort(A[:n//2])\n",
    "  R = mergesort(A[n//2:])\n",
    "\n",
    "  B = merge(L,R)\n",
    "\n",
    "  return(B)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "9gzgSw_DsZ9b"
   },
   "source": [
    "### A simple input to check correctness"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "4UPyJKpTtOmN",
    "outputId": "f2917286-7f3f-4495-b19f-2a9b1e56d9d2"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0,\n",
       " 1,\n",
       " 2,\n",
       " 3,\n",
       " 4,\n",
       " 5,\n",
       " 6,\n",
       " 7,\n",
       " 8,\n",
       " 9,\n",
       " 10,\n",
       " 11,\n",
       " 12,\n",
       " 13,\n",
       " 14,\n",
       " 15,\n",
       " 16,\n",
       " 17,\n",
       " 18,\n",
       " 19,\n",
       " 20,\n",
       " 21,\n",
       " 22,\n",
       " 23,\n",
       " 24,\n",
       " 25,\n",
       " 26,\n",
       " 27,\n",
       " 28,\n",
       " 29,\n",
       " 30,\n",
       " 31,\n",
       " 32,\n",
       " 33,\n",
       " 34,\n",
       " 35,\n",
       " 36,\n",
       " 37,\n",
       " 38,\n",
       " 39,\n",
       " 40,\n",
       " 41,\n",
       " 42,\n",
       " 43,\n",
       " 44,\n",
       " 45,\n",
       " 46,\n",
       " 47,\n",
       " 48,\n",
       " 49,\n",
       " 50,\n",
       " 51,\n",
       " 52,\n",
       " 53,\n",
       " 54,\n",
       " 55,\n",
       " 56,\n",
       " 57,\n",
       " 58,\n",
       " 59,\n",
       " 60,\n",
       " 61,\n",
       " 62,\n",
       " 63,\n",
       " 64,\n",
       " 65,\n",
       " 66,\n",
       " 67,\n",
       " 68,\n",
       " 69,\n",
       " 70,\n",
       " 71,\n",
       " 72,\n",
       " 73,\n",
       " 74,\n",
       " 75,\n",
       " 76,\n",
       " 77,\n",
       " 78,\n",
       " 79,\n",
       " 80,\n",
       " 81,\n",
       " 82,\n",
       " 83,\n",
       " 84,\n",
       " 85,\n",
       " 86,\n",
       " 87,\n",
       " 88,\n",
       " 89,\n",
       " 90,\n",
       " 91,\n",
       " 92,\n",
       " 93,\n",
       " 94,\n",
       " 95,\n",
       " 96,\n",
       " 97,\n",
       " 98,\n",
       " 99]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mergesort([i for i in range(0,100,2)]+[j for j in range (1,100,2)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "O8dFFhg3sZ9c"
   },
   "source": [
    "### Perfomance on large inputs, $10^6$, random and sorted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 0
    },
    "id": "WI6uw7A2sZ9c",
    "outputId": "ede4af5e-6ad4-4698-d85c-b90eb454bd5a"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "random 2.7101028859979124\n",
      "ascending 1.4198947430049884\n",
      "descending 1.4313436170050409\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "random.seed(2025)\n",
    "inputlists = {}\n",
    "inputlists[\"random\"] = [random.randrange(100000000) for i in range(1000000)]\n",
    "inputlists[\"ascending\"] = [i for i in range(1000000)]\n",
    "inputlists[\"descending\"] = [i for i in range (999999,-1,-1)]\n",
    "for k in inputlists.keys():\n",
    "    tmplist = inputlists[k][:]\n",
    "    starttime = time.perf_counter()\n",
    "    mergesort(tmplist)\n",
    "    elapsed = time.perf_counter() - starttime\n",
    "    print(k,elapsed)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Quicksort"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def quicksort(L,l,r):  # Sort L[l:r]\n",
    "  if (r - l <= 1):\n",
    "    return\n",
    "  (pivot,lower,upper) = (L[l],l+1,l+1)\n",
    "  for i in range(l+1,r):\n",
    "    if L[i] > pivot:  # Extend upper segment\n",
    "      upper = upper+1\n",
    "    else:  # Exchange L[i] with start of upper segment\n",
    "      (L[i], L[lower]) = (L[lower], L[i])\n",
    "      # Shift both segments\n",
    "      (lower,upper) = (lower+1,upper+1)\n",
    "  # Move pivot between lower and upper\n",
    "  (L[l],L[lower-1]) = (L[lower-1],L[l])\n",
    "  lower = lower-1\n",
    "  # Recursive calls\n",
    "  quicksort(L,l,lower)\n",
    "  quicksort(L,lower+1,upper)\n",
    "  return"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Small input to check correctness"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 3, 5, 0, 2, 2, 4, 17, -5, 6, 4, 3]\n"
     ]
    }
   ],
   "source": [
    "qlist = [1,3,5,0,2,4,17,2,-5,6,4,3]\n",
    "quicksort(qlist,4,8)\n",
    "print(qlist)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Quicksort performance\n",
    "- Random input of size $10^6$\n",
    "- Sorted inputs of size $2 \\times 10^4$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "random 1.9635030670033302\n",
      "ascending 4.193553910001356\n",
      "descending 5.847825593002199\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "random.seed(2025)\n",
    "inputlists = {}\n",
    "inputlists[\"random\"] = [random.randrange(100000000) for i in range(1000000)]\n",
    "inputlists[\"ascending\"] = [i for i in range(15000)]\n",
    "inputlists[\"descending\"] = [i for i in range (14999,-1,-1)]\n",
    "for k in inputlists.keys():\n",
    "    tmplist = inputlists[k][:]\n",
    "    starttime = time.perf_counter()\n",
    "    quicksort(tmplist,0,len(tmplist))\n",
    "    elapsed = time.perf_counter() - starttime\n",
    "    print(k,elapsed)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Randomized quicksort\n",
    "- Choose the pivot position uniformly at random between `l` and `r-1`\n",
    "- Swap pivot to `L[l]` and proceed as usual"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "def quicksortrand(L,l,r):  # Sort L[l:r]\n",
    "  if (r - l <= 1):\n",
    "    return(L)\n",
    "\n",
    "  # Choose a random postion between l and r-l as pivot, swap with L[l]\n",
    "  randpivot = random.randrange(r-l)\n",
    "  (L[l],L[l+randpivot]) = (L[l+randpivot],L[l])\n",
    " \n",
    "  # Rest is same as before\n",
    "  (pivot,lower,upper) = (L[l],l+1,l+1)\n",
    "  for i in range(l+1,r):\n",
    "    if L[i] > pivot:  # Extend upper segment\n",
    "      upper = upper+1\n",
    "    else:  # Exchange L[i] with start of upper segment\n",
    "      (L[i], L[lower]) = (L[lower], L[i])\n",
    "      # Shift both segments\n",
    "      (lower,upper) = (lower+1,upper+1)\n",
    "  # Move pivot between lower and upper\n",
    "  (L[l],L[lower-1]) = (L[lower-1],L[l])\n",
    "  lower = lower-1\n",
    "  # Recursive calls\n",
    "  quicksortrand(L,l,lower)\n",
    "  quicksortrand(L,lower+1,upper)\n",
    "  return(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "random 2.1547414349988685\n",
      "ascending 0.01822181899478892\n",
      "descending 0.018493495001166593\n",
      "ascendingbig 1.622358353000891\n",
      "descendingbig 1.747888535996026\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "random.seed(2025)\n",
    "inputlists = {}\n",
    "inputlists[\"random\"] = [random.randrange(100000000) for i in range(1000000)]\n",
    "inputlists[\"ascending\"] = [i for i in range(15000)]\n",
    "inputlists[\"descending\"] = [i for i in range (14999,-1,-1)]\n",
    "inputlists[\"ascendingbig\"] = [i for i in range(1000000)]\n",
    "inputlists[\"descendingbig\"] = [i for i in range (999999,-1,-1)]\n",
    "for k in inputlists.keys():\n",
    "    tmplist = inputlists[k][:]\n",
    "    starttime = time.perf_counter()\n",
    "    quicksortrand(tmplist,0,len(tmplist))\n",
    "    elapsed = time.perf_counter() - starttime\n",
    "    print(k,elapsed)"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "PDSP-Sep2021-Searching-Sorting.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
