{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "green-washer", "metadata": {}, "outputs": [], "source": [ "def insertion_sort(list_to_sort): # O(n^2)\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": null, "id": "photographic-punch", "metadata": {}, "outputs": [], "source": [ "import random\n", "from time import time\n", "import math" ] }, { "cell_type": "code", "execution_count": null, "id": "sustainable-float", "metadata": {}, "outputs": [], "source": [ "L = [random.randint(1,1000000) for n in range(10000)] " ] }, { "cell_type": "code", "execution_count": null, "id": "identified-awareness", "metadata": {}, "outputs": [], "source": [ "tt = time()\n", "R1 = insertion_sort(L)\n", "print(time()-tt)" ] }, { "cell_type": "code", "execution_count": null, "id": "forward-check", "metadata": {}, "outputs": [], "source": [ "tt = time()\n", "R2 = sorted(L)\n", "print(time()-tt)" ] }, { "cell_type": "code", "execution_count": null, "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": null, "id": "accepting-diana", "metadata": {}, "outputs": [], "source": [ "tt = time()\n", "R3 = merge_sort(L)\n", "print(time()-tt)" ] }, { "cell_type": "code", "execution_count": null, "id": "typical-algeria", "metadata": {}, "outputs": [], "source": [ "R3 == R2" ] }, { "cell_type": "code", "execution_count": null, "id": "automatic-season", "metadata": { "scrolled": true }, "outputs": [], "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": null, "id": "solar-shelf", "metadata": {}, "outputs": [], "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": null, "id": "checked-announcement", "metadata": {}, "outputs": [], "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 (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.11.1" } }, "nbformat": 4, "nbformat_minor": 5 }