Algomination
Data Structures
AboutContact

Algomination

Learn algorithms and data structures through smooth, interactive visualizations.

SortingSearchingArray AlgorithmsData StructuresAboutContact

© 2026Algomination. Created by Omang Rawat & Rahul Soni.

Omang Rawat
Rahul Soni
All array algorithms

Dutch National Flag (3-Way Partition)

Time O(n)Space O(1)

Partitions an array into three regions — less than, equal to, and greater than a pivot — in one pass with three pointers. The classic problem of sorting an array of 0s, 1s, and 2s.

9
4
7
4
2
4
8
1
4
6
Three-way partition around pivot 4: smaller values go left, equal stay in the middle, larger go right.
Step 1 / 12

About Dutch National Flag (3-Way Partition)

Partitions an array into three regions — less than, equal to, and greater than a pivot — in one pass with three pointers. The classic problem of sorting an array of 0s, 1s, and 2s.

Time complexity: O(n). Space complexity: O(1).

How it works, step by step

Core idea — Three pointers (low, mid, high) carve the array into a smaller-than region on the left, an equal region in the middle, and a greater-than region on the right.

What each pass accomplishes — Each step inspects the value at mid: smaller values swap left and both low and mid advance; larger values swap to the right and high retreats (mid stays to re-check the value that came back); equal values just advance mid. One pass fully partitions the array.

Use the interactive visualizer above to run Dutch National Flag (3-Way Partition) on your own input and watch every comparison, swap, and operation animate step by step — pause, scrub, or replay at any speed.