{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0fb833b0-7843-43d1-8812-2c53d5c9d651",
   "metadata": {},
   "source": [
    "### <center>San Jose State University<br>Department of Applied Data Science<br><br>**DATA 200<br>Computational Programming for Data Analytics**<br><br>Spring 2024<br>Instructor: Ron Mak</center>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "237dbc8a-f51b-4461-9cc6-f7058930460b",
   "metadata": {},
   "source": [
    "# Graphed Quicksort Timings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "992e6056-a30e-4d08-b100-842ddf065dde",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import random\n",
    "import time\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "from quicksort import Quicksort\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5cb9aaae-5eda-4bcf-ac5a-6b7d38f4c65f",
   "metadata": {},
   "outputs": [],
   "source": [
    "SIZES = list(range(0, 1_000_001, 100_000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "06d34311-ede6-47e4-a092-c3f23b12e079",
   "metadata": {},
   "outputs": [],
   "source": [
    "qs = Quicksort()\n",
    "\n",
    "quicksort_times = []\n",
    "sort_times = []\n",
    "np_times = []\n",
    "\n",
    "for size in SIZES:\n",
    "    print(f'{size = :9,d}')\n",
    "    master_list = [random.randint(0, size) for _ in range(size)];\n",
    "\n",
    "    unsorted_list = master_list.copy()\n",
    "    \n",
    "    start_time = time.time();\n",
    "    qs.sort(unsorted_list)\n",
    "    quicksort_times.append(time.time() - start_time)\n",
    "\n",
    "    unsorted_list = master_list.copy()\n",
    "\n",
    "    start_time = time.time();\n",
    "    unsorted_list.sort()\n",
    "    sort_times.append(time.time() - start_time)\n",
    "\n",
    "    unsorted_array = np.array(master_list.copy())\n",
    "\n",
    "    start_time = time.time();\n",
    "    np.sort(unsorted_array)\n",
    "    np_times.append(time.time() - start_time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67038a6a-6826-4806-a38b-a5c712f6204a",
   "metadata": {},
   "outputs": [],
   "source": [
    "for t in quicksort_times:\n",
    "    print(f'{t:20.18f}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a05e97b-f633-49ac-b1d9-68f370e3a238",
   "metadata": {},
   "outputs": [],
   "source": [
    "for t in sort_times:\n",
    "    print(f'{t:20.18f}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8fbf2872-3c90-4725-a9db-48f6bfd45fb4",
   "metadata": {},
   "outputs": [],
   "source": [
    "for t in np_times:\n",
    "    print(f'{t:20.18f}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4001e705-0f41-4419-be3e-585e111730b8",
   "metadata": {},
   "source": [
    "## Quicksort time is O(*n*log(*n*))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a75bc01-faeb-4e94-89e2-e31afafa5636",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.set()\n",
    "\n",
    "plt.figure(figsize=(6, 10))\n",
    "X = list(range(0, 11))\n",
    "\n",
    "ax = sns.lineplot(x=X, y=quicksort_times, label='quicksort')\n",
    "ax = sns.lineplot(x=X, y=sort_times,      label='sort()')\n",
    "ax = sns.lineplot(x=X, y=np_times,        label='np.sort()')\n",
    "\n",
    "ax.set_yticks([y/10 for y in range(0, 30)])\n",
    "\n",
    "plt.title('Quicksort vs sort() vs np.sort()')\n",
    "plt.xlabel('Sizes in 100,000s')\n",
    "plt.ylabel('Sort time (seconds)')\n",
    "\n",
    "plt.show()\n",
    "plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12985db5-ad7c-4677-b90d-3f3c8e66e2e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# (c) 2024 by Ronald Mak"
   ]
  }
 ],
 "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
