{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "green-washer", "metadata": {}, "outputs": [], "source": [ "def insertion_sort(list_to_sort):\n", " L = list(list_to_sort)\n", " new_list = []\n", " \n", " while len(L) > 0:\n", " # find the index of the smallest thing\n", " min_index = min(range(len(L)), key=lambda i : L[i])\n", " \n", " # remove it from L and add it to new_list\n", " new_list.append(L.pop(min_index))\n", " \n", " return new_list" ] }, { "cell_type": "code", "execution_count": 2, "id": "photographic-punch", "metadata": {}, "outputs": [], "source": [ "import random\n", "from time import time\n", "import math" ] }, { "cell_type": "code", "execution_count": 3, "id": "sustainable-float", "metadata": {}, "outputs": [], "source": [ "L = [random.randint(1,1000000) for n in range(10000)] " ] }, { "cell_type": "code", "execution_count": 4, "id": "identified-awareness", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5.3559629917144775\n" ] } ], "source": [ "tt = time()\n", "R1 = insertion_sort(L)\n", "print(time()-tt)" ] }, { "cell_type": "code", "execution_count": 5, "id": "forward-check", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.00138092041015625\n" ] } ], "source": [ "tt = time()\n", "R2 = sorted(L)\n", "print(time()-tt)" ] }, { "cell_type": "code", "execution_count": 6, "id": "wired-vermont", "metadata": {}, "outputs": [], "source": [ "def merge_sort(L):\n", " \n", " # base case: a list of length 1 is already sorted\n", " if len(L) <= 1:\n", " return L\n", " \n", " # split the list (roughly) in hald\n", " mid_point = math.ceil(len(L)/2)\n", " left_half = L[:mid_point]\n", " right_half = L[mid_point:]\n", " \n", " # recursively apply the function to the two halfs (divide)\n", " LS = merge_sort(left_half)\n", " RS = merge_sort(right_half)\n", " \n", " # time to conquer\n", " full_list = []\n", " \n", " # while the two halves still have something left\n", " while len(LS) + len(RS) > 0:\n", " # either LS[0] or RS[0] is the smallest of all elements\n", " # in LS and RS. Find it, remove it from its list, and\n", " # add it to full_list\n", " if LS:\n", " if RS:\n", " if LS[0] <= RS[0]:\n", " full_list.append(LS.pop(0))\n", " else:\n", " full_list.append(RS.pop(0))\n", " else:\n", " full_list.append(LS.pop(0))\n", " else:\n", " full_list.append(RS.pop(0))\n", " return full_list\n", " " ] }, { "cell_type": "code", "execution_count": 7, "id": "accepting-diana", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.06351804733276367\n" ] } ], "source": [ "tt = time()\n", "R3 = merge_sort(L)\n", "print(time()-tt)" ] }, { "cell_type": "code", "execution_count": 8, "id": "typical-algeria", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "R3 == R2" ] }, { "cell_type": "code", "execution_count": 12, "id": "automatic-season", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 0.000209808349609375\n", "100 0.00075531005859375\n", "\t3.6775320139697323\n", "1000 0.06404900550842285\n", "\t85.0360873694207\n", "10000 5.391217947006226\n", "\t84.17702854122228\n" ] }, { "ename": "KeyboardInterrupt", "evalue": "", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mtt\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mR\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minsertion_sort\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mL\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 7\u001b[0m \u001b[0mtimes\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mtt\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36minsertion_sort\u001b[0;34m(list_to_sort)\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mL\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;31m# find the index of the smallest thing\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mmin_index\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mL\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mlambda\u001b[0m \u001b[0mi\u001b[0m \u001b[0;34m:\u001b[0m \u001b[0mL\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;31m# remove it from L and add it to new_list\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m(i)\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mL\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;31m# find the index of the smallest thing\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mmin_index\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mL\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mlambda\u001b[0m \u001b[0mi\u001b[0m \u001b[0;34m:\u001b[0m \u001b[0mL\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;31m# remove it from L and add it to new_list\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mKeyboardInterrupt\u001b[0m: " ] } ], "source": [ "times = []\n", "for p in range(1,6):\n", " L = [random.randint(1,1000000) for n in range(10**p)] \n", " \n", " tt = time()\n", " R = insertion_sort(L)\n", " times.append(time()-tt)\n", " \n", " print(f\"{10**p} {time()-tt}\")\n", " if len(times) > 1:\n", " print(f\"\\t{times[-1]/times[-2]}\")" ] }, { "cell_type": "code", "execution_count": 11, "id": "solar-shelf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 0.0023648738861083984\n", "100 0.0004220008850097656\n", "\t0.17847397675593735\n", "1000 0.005793094635009766\n", "\t13.746319365798415\n", "10000 0.07274603843688965\n", "\t12.568215521502719\n", "100000 1.458730936050415\n", "\t20.05314546419933\n" ] } ], "source": [ "times = []\n", "for p in range(1,6):\n", " L = [random.randint(1,1000000) for n in range(10**p)] \n", " \n", " tt = time()\n", " R2 = merge_sort(L)\n", " times.append(time()-tt)\n", " \n", " print(f\"{10**p} {time()-tt}\")\n", " if len(times) > 1:\n", " print(f\"\\t{times[-1]/times[-2]}\")" ] }, { "cell_type": "code", "execution_count": 13, "id": "checked-announcement", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 9.799003601074219e-05\n", "100 1.5020370483398438e-05\n", "\t0.13819095477386933\n", "1000 0.0001399517059326172\n", "\t10.50909090909091\n", "10000 0.0017790794372558594\n", "\t12.865051903114187\n", "100000 0.017344951629638672\n", "\t9.780661646046262\n", "1000000 0.3232860565185547\n", "\t18.643429718544184\n", "10000000 4.332744836807251\n", "\t13.402582604552782\n" ] } ], "source": [ "times = []\n", "for p in range(1,8):\n", " L = [random.randint(1,1000000) for n in range(10**p)] \n", " \n", " tt = time()\n", " R3 = sorted(L)\n", " times.append(time()-tt)\n", " \n", " print(f\"{10**p} {time()-tt}\")\n", " if len(times) > 1:\n", " print(f\"\\t{times[-1]/times[-2]}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "rough-breakdown", "metadata": {}, "outputs": [], "source": [] } ], "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.9.1" } }, "nbformat": 4, "nbformat_minor": 5 }