QuickSelect with Hoare partition scheme
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
QuickSelect is an efficient algorithm used to find the k-th smallest element in an unordered list or array. It shares conceptual similarities with QuickSort, particularly in the way it partitions the data. One common partitioning method it uses is the Hoare partition scheme. Understanding QuickSelect with the Hoare partition scheme provides insight into both efficient selection algorithms and array partitioning techniques.
Overview of QuickSelect Algorithm
QuickSelect is a selection algorithm that works in an average time complexity of , but it can degrade to in the worst-case scenario. Despite the possibility of the worst-case scenario, its average-case efficiency makes it a popular choice for selection problems.
Steps of QuickSelect
- Select a Pivot: Choose a pivot element from the array. Unlike QuickSort, where the choice of pivot can significantly affect performance, QuickSelect performance primarily hinges on finding the correct partition rather than sorting.
- Partition the Array: Rearrange the elements such that all elements less than or equal to the pivot are on its left, and all elements greater or equal are on its right. This step is where the Hoare partition scheme plays a critical role.
- Recurse Depending on Position:
- If the pivot’s position equals
k, return the pivot as it is the k-th smallest element. - If
kis smaller than the pivot's index, recursively apply QuickSelect to the left sub-array. - If
kis larger, apply it to the right sub-array.
Hoare Partition Scheme
The Hoare partition scheme is one of the simplest yet effective partitioning schemes, devised by C.A.R. Hoare, who also created QuickSort. The key advantage of the Hoare scheme is that it performs fewer swaps compared to other schemes, like Lomuto.
Steps of the Hoare Partitioning
- Initialization:
- Choose pivot (usually the first element for simplicity).
- Initialize two pointers:
istarting from the beginning of the array, andjfrom the end.
- Move Pointers:
- Increment
iuntil you find an element greater than or equal to the pivot. - Decrement
juntil you find an element less than or equal to the pivot.
- Swap and Repeat:
- Swap elements at
iandj. - Repeat moving the pointers and swapping until
imeets or surpassesj. - The array is now partitioned into elements less than the pivot, elements equal to the pivot, and elements greater than the pivot.
Example
Suppose we have the array [8, 3, 5, 4, 6, 2, 7] and we need to find the 3rd smallest element:
- Increment
i: No need to move as 8 is greater than 3. - Decrement
j: Continue until it points to the last element less or equal to 8 (2 in this case). - Swap
8with2. Now the array is[2, 3, 5, 4, 6, 8, 7]. - Proceed with similar steps to fully partition around
2. - Efficient for finding order statistics without full sorting.
- In-place with no additional space requirements.
- Simpler and fewer swaps with Hoare partitioning, leading to a generally faster execution than Lomuto partitioning in practice.
- Used in Median of Medians algorithm.
- Fundamental for quick statistical analyses of unsorted data.
- Basis for competitive programming solutions where time complexity is crucial.

