Quicksort
3-way partition
sorting algorithms
computer science
algorithm optimization

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.

Practice algorithms

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:

  1. 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.
  2. 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.
  3. 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:

  1. Elements Less Than the Pivot: Stored to the left.
  2. Elements Equal to the Pivot: Placed in the middle.
  3. 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 O(nlogn)O(n \log n).
  • Efficiency with Duplicates: The primary advantage is efficiency in handling arrays with many duplicate elements.
  • Time Complexity: Though the worst-case time complexity remains O(n2)O(n^2), 3-way partitioning improves performance in practice for arrays with many duplicates.
  • Space Complexity: Like standard Quicksort, it can work in-place, consuming O(logn)O(\log n) space due to the recursion stack.

Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.