Appendix B — Quicksort
Partitioning an Array
\begin{algorithm} \caption{PARTITION} \begin{algorithmic} \Procedure{Partition}{$A, p, r$} \State{$x=A[r]$}\Comment{The pivot} \State{$i=p-1$}\Comment{Highest index into the low side} \For{$j=p$ to $r-1$}\Comment{Process each element other than the pivot} \If{$A[j]\leq x$}\Comment{Does this element belong on the low side?} \State{$i = i+1$}\Comment{Index of a new slot in the low side} \State{exchange $A[i]$ and $A[j]$}\Comment{Put this element there} \EndIf \EndFor \State{exchange $A[i+1]$ and $A[r]$}\Comment{Pivot goes just to the right of the low side} \Return{$i+1$} \EndProcedure \end{algorithmic} \end{algorithm}
Demo
Quicksort Algorithm
\begin{algorithm} \caption{QuickSort} \begin{algorithmic} \Procedure{QuickSort}{$A, p, r$} \If{$p\leq r$} \State{} \Comment{Partition the subarray around the pivot, which ends up in $A[q]$} \State{$q=$\Call{Partition}{$A, p, r$}} \State{\Call{QuickSort}{$A, p, q - 1$}} \Comment{Recursively sort the low side} \State{\Call{QuickSort}{$A, q + 1, r$}} \Comment{Recursively sort the high side} \EndIf \EndProcedure \end{algorithmic} \end{algorithm}