avatar khanh than

SOFTWARE ENGINEER

To keep tracking my skills & accomplisments

20 Oct 2024

● Algorithm

Quicksort By Hoare And Lomuto

Quicksort is a widely used divide-and-conquer algorithm for sorting an array ${A[1,2,…,n]}$. The key concept is the partitioning of the array into 2 subarrays based on a selected pivot.

There are two solutions named: Hoare & Lomuto


$\pmb {Hoare \ ‘s \ Solution:}$

  1. Select the first item as an initial pivot: ${p = A(1)}$.

  2. Indices: initialize ${i = 1 \ and \ j = n}$.

  3. Loop: while i $\lt$ j:
    • ${i \leftarrow i+1 \ while \ A(i) \lt p}$.
    • ${j \leftarrow j-1 \ while \ A(j) \ge p}$.
    • if ${i \lt j: \ swap \ A(i) \ and \ A(j)}$.

    (the scheme will be ${|A_i|A_{i+1}|...|A_{j-1}|A_j|}$, this swapping is to move the smaller items to the leftside and the greater items to the rightside of the pivot).

    Partition result: with the partition index is $j$, and the array is divided into two arrays: ${A_{left} \ and \ A_{right}}$:

    • ${A_{left} (k) \le p, \ \ \ \forall k = 1,2,…,j}$.
    • ${A_{right} (k) \ge p, \ \forall k = j+1,j+2,…,n}$.
    • And the pivot $\{ p \}$ at index $j$.

  4. Recursion: recusively apply quicksort to the two arrays:
    • Result ${=}$ quicksort($A_{left}$) $\cup$ $\{ p \}$ $\cup$ quicksort($A_{right}$).
    • Condition to stop the recursion is when the array size n ${\le}$ 1, it is because this input array is fully-sorted naturally at this time.


$\pmb {Lomuto \ ‘s \ Solution:}$

  1. Select the final item as an initial pivot: ${p = A(n)}$.

  2. Indices: initialize ${i = 1}$.

  3. Loop: for $j=1$ to $n$:
    • if $A(j) \le p,$ swap $A(i)$ & $A(j)$, then increment $i \leftarrow i+1$.

    Result: j always run faster than i, so that this swapping is to move the smaller items to the leftside of the imagined pivot. Hence, with the partition index is $(i+1)$, the array is divided as:

    • ${A_{left} (k) \le p, \ \ \ \forall k = 1,2,…,i}$.
    • ${A_{right} (k) \gt p, \ \forall k = i+2,…,n-1}$.
    • The pivot ${\{p \}}$ at index $i+1$.

  4. Recursion: recusively apply quicksort to the two arrays:
    • Result ${=}$ quicksort($A_{left}$) $\cup$ $\{p\}$ $\cup$ quicksort($A_{right}$).
    • Condition to stop the recursion is when the array size n ${\le}$ 1, it is because this input array is fully-sorted naturally at this time.



$\pmb {Time \ complexity:}$

Call $T(n)$ to be the time complexity for sorting an array of size n. It represents the exact amount of time or number of operations that an algorithm takes with an input size n:

REMARK: Although the time complexity of the Hoare and Lomuto solutions look like the same, but the Lomuto partition index moves from the left to the right quite slowly, which could cause the partitioning to be unbalanced and easily to become a “worst case”. This results in a higher number of swaps or a higher time complexity in the Lomuto solution compared to the Hoare solution, making Lomuto quicksort slightly more expensive.

Share on: