import random\n",
"\n",
"def quicksort(arr, low=0, high=None):\n",
" if high is None:\n",
" high = len(arr) - 1\n",
"\n",
" if low < high:\n",
" pivot_index = partition(arr, low, high)\n",
" quicksort(arr, low, pivot_index - 1)\n",
" quicksort(arr, pivot_index + 1, high)\n",
"\n",
"def partition(arr, low, high):\n",
" pivot_index = random.randint(low, high)\n",
" pivot = arr[pivot_index]\n",
" arr[pivot_index], arr[high] = arr[high], arr[pivot_index] # Move pivot to end\n",
" i = low\n",
" for j in range(low, high):\n",
" if arr[j] < pivot:\n",
" arr[i], arr[j] = arr[j], arr[i]\n",
" i += 1\n",
" arr[i], arr[high] = arr[high], arr[i] # Move pivot to its final place\n",
" return i\n",
"\n",
"# Test the quicksort function\n",
"sample_array = [3, 6, 8, 10, 1, 2, 1]\n",
"quicksort(sample_array)\n",
"print(sample_array)\n",
" \n"
],
"text/latex": [
"\\begin{Verbatim}[commandchars=\\\\\\{\\}]\n",
"\\PY{k+kn}{import}\\PY{+w}{ }\\PY{n+nn}{random}\n",
"\n",
"\\PY{k}{def}\\PY{+w}{ }\\PY{n+nf}{quicksort}\\PY{p}{(}\\PY{n}{arr}\\PY{p}{,} \\PY{n}{low}\\PY{o}{=}\\PY{l+m+mi}{0}\\PY{p}{,} \\PY{n}{high}\\PY{o}{=}\\PY{k+kc}{None}\\PY{p}{)}\\PY{p}{:}\n",
" \\PY{k}{if} \\PY{n}{high} \\PY{o+ow}{is} \\PY{k+kc}{None}\\PY{p}{:}\n",
" \\PY{n}{high} \\PY{o}{=} \\PY{n+nb}{len}\\PY{p}{(}\\PY{n}{arr}\\PY{p}{)} \\PY{o}{\\PYZhy{}} \\PY{l+m+mi}{1}\n",
"\n",
" \\PY{k}{if} \\PY{n}{low} \\PY{o}{\\PYZlt{}} \\PY{n}{high}\\PY{p}{:}\n",
" \\PY{n}{pivot\\PYZus{}index} \\PY{o}{=} \\PY{n}{partition}\\PY{p}{(}\\PY{n}{arr}\\PY{p}{,} \\PY{n}{low}\\PY{p}{,} \\PY{n}{high}\\PY{p}{)}\n",
" \\PY{n}{quicksort}\\PY{p}{(}\\PY{n}{arr}\\PY{p}{,} \\PY{n}{low}\\PY{p}{,} \\PY{n}{pivot\\PYZus{}index} \\PY{o}{\\PYZhy{}} \\PY{l+m+mi}{1}\\PY{p}{)}\n",
" \\PY{n}{quicksort}\\PY{p}{(}\\PY{n}{arr}\\PY{p}{,} \\PY{n}{pivot\\PYZus{}index} \\PY{o}{+} \\PY{l+m+mi}{1}\\PY{p}{,} \\PY{n}{high}\\PY{p}{)}\n",
"\n",
"\\PY{k}{def}\\PY{+w}{ }\\PY{n+nf}{partition}\\PY{p}{(}\\PY{n}{arr}\\PY{p}{,} \\PY{n}{low}\\PY{p}{,} \\PY{n}{high}\\PY{p}{)}\\PY{p}{:}\n",
" \\PY{n}{pivot\\PYZus{}index} \\PY{o}{=} \\PY{n}{random}\\PY{o}{.}\\PY{n}{randint}\\PY{p}{(}\\PY{n}{low}\\PY{p}{,} \\PY{n}{high}\\PY{p}{)}\n",
" \\PY{n}{pivot} \\PY{o}{=} \\PY{n}{arr}\\PY{p}{[}\\PY{n}{pivot\\PYZus{}index}\\PY{p}{]}\n",
" \\PY{n}{arr}\\PY{p}{[}\\PY{n}{pivot\\PYZus{}index}\\PY{p}{]}\\PY{p}{,} \\PY{n}{arr}\\PY{p}{[}\\PY{n}{high}\\PY{p}{]} \\PY{o}{=} \\PY{n}{arr}\\PY{p}{[}\\PY{n}{high}\\PY{p}{]}\\PY{p}{,} \\PY{n}{arr}\\PY{p}{[}\\PY{n}{pivot\\PYZus{}index}\\PY{p}{]} \\PY{c+c1}{\\PYZsh{} Move pivot to end}\n",
" \\PY{n}{i} \\PY{o}{=} \\PY{n}{low}\n",
" \\PY{k}{for} \\PY{n}{j} \\PY{o+ow}{in} \\PY{n+nb}{range}\\PY{p}{(}\\PY{n}{low}\\PY{p}{,} \\PY{n}{high}\\PY{p}{)}\\PY{p}{:}\n",
" \\PY{k}{if} \\PY{n}{arr}\\PY{p}{[}\\PY{n}{j}\\PY{p}{]} \\PY{o}{\\PYZlt{}} \\PY{n}{pivot}\\PY{p}{:}\n",
" \\PY{n}{arr}\\PY{p}{[}\\PY{n}{i}\\PY{p}{]}\\PY{p}{,} \\PY{n}{arr}\\PY{p}{[}\\PY{n}{j}\\PY{p}{]} \\PY{o}{=} \\PY{n}{arr}\\PY{p}{[}\\PY{n}{j}\\PY{p}{]}\\PY{p}{,} \\PY{n}{arr}\\PY{p}{[}\\PY{n}{i}\\PY{p}{]}\n",
" \\PY{n}{i} \\PY{o}{+}\\PY{o}{=} \\PY{l+m+mi}{1}\n",
" \\PY{n}{arr}\\PY{p}{[}\\PY{n}{i}\\PY{p}{]}\\PY{p}{,} \\PY{n}{arr}\\PY{p}{[}\\PY{n}{high}\\PY{p}{]} \\PY{o}{=} \\PY{n}{arr}\\PY{p}{[}\\PY{n}{high}\\PY{p}{]}\\PY{p}{,} \\PY{n}{arr}\\PY{p}{[}\\PY{n}{i}\\PY{p}{]} \\PY{c+c1}{\\PYZsh{} Move pivot to its final place}\n",
" \\PY{k}{return} \\PY{n}{i}\n",
"\n",
"\\PY{c+c1}{\\PYZsh{} Test the quicksort function}\n",
"\\PY{n}{sample\\PYZus{}array} \\PY{o}{=} \\PY{p}{[}\\PY{l+m+mi}{3}\\PY{p}{,} \\PY{l+m+mi}{6}\\PY{p}{,} \\PY{l+m+mi}{8}\\PY{p}{,} \\PY{l+m+mi}{10}\\PY{p}{,} \\PY{l+m+mi}{1}\\PY{p}{,} \\PY{l+m+mi}{2}\\PY{p}{,} \\PY{l+m+mi}{1}\\PY{p}{]}\n",
"\\PY{n}{quicksort}\\PY{p}{(}\\PY{n}{sample\\PYZus{}array}\\PY{p}{)}\n",
"\\PY{n+nb}{print}\\PY{p}{(}\\PY{n}{sample\\PYZus{}array}\\PY{p}{)}\n",
"\\end{Verbatim}\n"
],
"text/plain": [
"import random\n",
"\n",
"def quicksort(arr, low=0, high=None):\n",
" if high is None:\n",
" high = len(arr) - 1\n",
"\n",
" if low < high:\n",
" pivot_index = partition(arr, low, high)\n",
" quicksort(arr, low, pivot_index - 1)\n",
" quicksort(arr, pivot_index + 1, high)\n",
"\n",
"def partition(arr, low, high):\n",
" pivot_index = random.randint(low, high)\n",
" pivot = arr[pivot_index]\n",
" arr[pivot_index], arr[high] = arr[high], arr[pivot_index] # Move pivot to end\n",
" i = low\n",
" for j in range(low, high):\n",
" if arr[j] < pivot:\n",
" arr[i], arr[j] = arr[j], arr[i]\n",
" i += 1\n",
" arr[i], arr[high] = arr[high], arr[i] # Move pivot to its final place\n",
" return i\n",
"\n",
"# Test the quicksort function\n",
"sample_array = [3, 6, 8, 10, 1, 2, 1]\n",
"quicksort(sample_array)\n",
"print(sample_array)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.display import display, Markdown, Code\n",
"\n",
"display(Code(code, language=\"python\"))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"The quicksort function is a recursive sorting algorithm that sorts an array in-place. It works by selecting a 'pivot' element and partitioning the array into elements less than the pivot and elements greater than the pivot. The function then recursively sorts the partitions. A random pivot is chosen to improve performance on average, reducing the risk of worst-case time complexity. The partitioning is done in-place to save space. The base case for the recursion is when the sub-array has one or zero elements, which are already sorted. The test demonstrates the function by sorting a sample array."
],
"text/plain": [
"