{ "cells": [ { "cell_type": "markdown", "id": "7a0fe5ac-dd3e-456a-aafa-196373f0e70e", "metadata": {}, "source": [ "# Quicksort Implementation\n", "\n", "The quicksort implementation is very straightforward! The whole thing is below." ] }, { "cell_type": "code", "execution_count": 1, "id": "acd168ed-b77b-4717-87f9-352f990c86a0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[52, 42, 87, 15, 83, 66, 60, 40, 70, 14]\n" ] } ], "source": [ "import random\n", "\n", "N = 10\n", "array = [random.randint(0,100) for _ in range(N)]\n", "print(array)" ] }, { "cell_type": "code", "execution_count": null, "id": "7691bb19-fd38-4279-9ac1-d93ea8b35c2e", "metadata": {}, "outputs": [], "source": [ "def partition(A, start, end):\n", " pivot = A[end-1]\n", " i = start\n", " for j in range(start, end-1):\n", " if A[j] < pivot:\n", " A[i], A[j] = A[j], A[i]\n", " i += 1\n", " A[i], A[end-1] = A[end-1], A[i]\n", " return i\n", "\n", "def quicksort(A, start, end):\n", " if start>=end:\n", " return\n", " pivot = partition(A, start, end)\n", " quicksort(A, start, pivot)\n", " quicksort(A, pivot+1, end)" ] }, { "cell_type": "code", "execution_count": null, "id": "34267ff3-22d3-4fdb-bcd5-4c5dea3d6004", "metadata": {}, "outputs": [], "source": [ "quicksort(array, 0, len(array))\n", "print(array)" ] }, { "cell_type": "code", "execution_count": null, "id": "89842da8-0244-4e92-9b68-49b4ec38d4e0", "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.13.5" } }, "nbformat": 4, "nbformat_minor": 5 }