Quicksort with 3-way 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.
Introduction
Quicksort is a highly efficient and commonly used sorting algorithm in computer science. Initially developed by Tony Hoare in 1960, it follows the divide-and-conquer paradigm to sort an array by partitioning it into subarrays. One of the more sophisticated variations is Quicksort with 3-way partitioning, which handles duplicate elements more efficiently.
Basics of Quicksort
Quicksort follows these essential steps:
- Choose a Pivot: Selecting a pivot is crucial and can be done using several strategies like selecting the first, last, middle, or a random element.
- Partition the Array: Rearrange elements so that all elements less than the pivot are on the left, and those greater than the pivot are on the right. The pivot itself is placed between the two partitions.
- Recursively Apply: Apply the above steps to the subarrays on the left and right of the pivot.
Challenges with Duplicate Elements
Standard Quicksort can perform poorly with large numbers of duplicate elements because it does not efficiently deal with elements equal to the pivot. This is where 3-way partitioning is advantageous.
3-Way Partition Quicksort
3-way Quicksort, also known as Dutch National Flag Quicksort, partitions the array into three sections:
- Elements Less Than the Pivot: Stored to the left.
- Elements Equal to the Pivot: Placed in the middle.
- Elements Greater Than the Pivot: Positioned on the right.
Implementation Details
Here's a simple implementation of Quicksort with 3-way partitioning in Python:
- Partitioning Function: The key is the `partition_3way` function which maintains three pointers:
- `lt` for elements less than the pivot
- `i` for elements being processed equal to the pivot
- `gt` for elements greater than the pivot
- Complexity: The 3-way partitioning ensures that elements equal to the pivot aren’t processed again, improving average performance to .
- Efficiency with Duplicates: The primary advantage is efficiency in handling arrays with many duplicate elements.
- Time Complexity: Though the worst-case time complexity remains , 3-way partitioning improves performance in practice for arrays with many duplicates.
- Space Complexity: Like standard Quicksort, it can work in-place, consuming space due to the recursion stack.
Related reading
- Quicksort with Python
- Quorum vs Consensus vs Vector Clock
- Radial Tree layout algorithm
- Radix sort LSD versus MSD versions
- Quiescent State Based Reclamation vs Epoch Based Reclamation
- RabbitMQ - How many queues can RabbitMQ handle on a single server?
- Radix sort vs Counting sort vs Bucket sort. What's the difference?
- Raft leader election algorithm one vote for term?

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.