Hoare partition (Given : pivot = first element. Generally we can take any elememt as pivot)
Counting rules :
- Count every pivot-vs-element comparison inside scans (including pivot with itself).
- Do not count "Stopping test $i \ge j$ comparisons".
Notation
Blue box = pointer $i$
Red box = pointer $j$
Nested box = $i=j$
Algorithm (one partition on $A[L..R]$)
pivot $= A[L]$, $i=L$, $j=R$
repeat
i-scan: while $A[i]<$ pivot do $i\leftarrow i+1$
j-scan: while $A[j]>$ pivot do $j\leftarrow j-1$
if $i<j$ then swap $A[i],A[j]$, $i\leftarrow i+1$, $j\leftarrow j-1$
else stop
Case 2: Input $[4,1,5,3,2]$
Partition on $[4,1,5,3,2]$, pivot $=4$
Start
\[
\begin{array}{ccccc}
\color{blue}{\boxed{4}} & 1 & 5 & 3 & \color{red}{\boxed{2}}
\end{array}
\]
Step 1: i-scan
$4<4$ (false)
Comparisons = 1
\[
\begin{array}{ccccc}
\color{blue}{\boxed{4}} & 1 & 5 & 3 & \color{red}{\boxed{2}}
\end{array}
\]
Step 2: j-scan
$2>4$ (false)
Comparisons = 1
\[
\begin{array}{ccccc}
\color{blue}{\boxed{4}} & 1 & 5 & 3 & \color{red}{\boxed{2}}
\end{array}
\]
Swap 1: swap 4 and 2
\[
\begin{array}{ccccc}
\color{blue}{\boxed{2}} & 1 & 5 & 3 & \color{red}{\boxed{4}}
\end{array}
\]
Update pointers: $i\leftarrow i+1,\ j\leftarrow j-1$
\[
\begin{array}{ccccc}
2 & \color{blue}{\boxed{1}} & 5 & \color{red}{\boxed{3}} & 4
\end{array}
\]
Step 3: i-scan
$1<4$ (true), $5<4$ (false)
Comparisons = 2
\[
\begin{array}{ccccc}
2 & 1 & \color{blue}{\boxed{5}} & \color{red}{\boxed{3}} & 4
\end{array}
\]
Step 4: j-scan
$3>4$ (false)
Comparisons = 1
\[
\begin{array}{ccccc}
2 & 1 & \color{blue}{\boxed{5}} & \color{red}{\boxed{3}} & 4
\end{array}
\]
Swap 2: swap 5 and 3
\[
\begin{array}{ccccc}
2 & 1 & \color{blue}{\boxed{3}} & \color{red}{\boxed{5}} & 4
\end{array}
\]
Update pointers: $i\leftarrow i+1,\ j\leftarrow j-1$
\[
\begin{array}{ccccc}
2 & 1 & \color{red}{\boxed{3}} & \color{blue}{\boxed{5}} & 4
\end{array}
\]
Partition-1 totals
Comparisons $=1+1+2+1=5$
Swaps $=2$
Subarrays: $[2,1,3]$ and $[5,4]$
Partition on $[2,1,3]$, pivot $=2$
Start
\[
\begin{array}{ccc}
\color{blue}{\boxed{2}} & 1 & \color{red}{\boxed{3}}
\end{array}
\]
Step 1: i-scan
$2<2$ (false)
Comparisons = 1
\[
\begin{array}{ccc}
\color{blue}{\boxed{2}} & 1 & \color{red}{\boxed{3}}
\end{array}
\]
Step 2: j-scan
$3>2$ (true), $1>2$ (false)
Comparisons = 2
\[
\begin{array}{ccc}
\color{blue}{\boxed{2}} & \color{red}{\boxed{1}} & 3
\end{array}
\]
Swap: swap 2 and 1
\[
\begin{array}{ccc}
\color{blue}{\boxed{1}} & \color{red}{\boxed{2}} & 3
\end{array}
\]
Update pointers
\[
\begin{array}{ccc}
\color{red}{\boxed{1}} & \color{blue}{\boxed{2}} & 3
\end{array}
\]
Partition-2 totals
Comparisons $=1+2=3$
Swaps $=1$
Remaining subarray $[2,3]$
Partition on $[2,3]$, pivot $=2$
Start
\[
\begin{array}{cc}
\color{blue}{\boxed{2}} & \color{red}{\boxed{3}}
\end{array}
\]
Step 1: i-scan
$2<2$ (false)
Comparisons = 1
\[
\begin{array}{cc}
\color{blue}{\boxed{2}} & \color{red}{\boxed{3}}
\end{array}
\]
Step 2: j-scan
$3>2$ (true), $2>2$ (false)
Comparisons = 2
$i=j$ (double box)
\[
\begin{array}{cc}
\color{blue}{\boxed{\color{red}{\boxed{2}}}} & 3
\end{array}
\]
Partition-3 totals
Comparisons $=1+2=3$
Swaps $=0$
Partition on $[5,4]$, pivot $=5$
Start
\[
\begin{array}{cc}
\color{blue}{\boxed{5}} & \color{red}{\boxed{4}}
\end{array}
\]
Step 1: i-scan
$5<5$ (false)
Comparisons = 1
Step 2: j-scan
$4>5$ (false)
Comparisons = 1
Swap: swap 5 and 4
\[
\begin{array}{cc}
\color{blue}{\boxed{4}} & \color{red}{\boxed{5}}
\end{array}
\]
Partition-4 totals
Comparisons $=2$
Swaps $=1$
Case 2 total comparisons
\[
t_2 = 5+3+3+2 = 13
\]