Quicksort pivot position after one partition
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Quicksort is a widely used and efficient sorting algorithm, best known for its divide-and-conquer approach. The core idea behind Quicksort is to select a "pivot" element from the array and partition the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. The process is repeated recursively to efficiently sort the list. A critical part of understanding Quicksort is grasping the pivot position after one partition.
Understanding the Pivot Position After One Partition
When implementing Quicksort, determining the pivot position after a single partition is crucial. This position effectively separates the array into two parts: elements smaller than the pivot and those larger.
Partitioning Process
The partitioning process works as follows:
- Choose a pivot element from the array. This choice can significantly affect the algorithm's performance. Common strategies are picking the first element, the last element, a random element, or the median.
- Rearrange the array elements so that all elements less than the pivot come before it and all greater elements come after it.
- The pivot element is now in its final position, and this position splits the array into two partitions.
Example of the Partitioning
Consider the following array:
- Start with two pointers: `low` starts at the beginning, and `high` moves from the end toward the start.
- Move `low` rightward until an element greater or equal to the pivot is found.
- Move `high` leftward until an element smaller or equal to the pivot is found.
- If `low` is less than or equal to `high`, swap these elements.
- Repeat until `low` exceeds `high`.
- Swap the pivot with the element at the `high` pointer.
- Best Case: Occurs when the pivot consistently splits the array into two equal halves. The time complexity is .
- Average Case: The time complexity remains , attributed to random pivot selection.
- Worst Case: Happens when partitioning results in highly unbalanced partitions, such as when the smallest or largest element is consistently chosen as a pivot. This results in a time complexity of .
- First or Last Element: Simple but can often lead to poor performance.
- Randomized Pivot: Helps avoid worst-case scenarios by reducing chances of unbalanced partitions.
- Median of Three: Typically involves picking the median of the first, middle, and last elements, providing a good balance.
Related reading

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.