Split values into less, equal and greater in one pass.
Every value before low is 0, from low to mid - 1 is 1, after high is 2, and mid to high is the unknown region.
Use it when values fall into three groups and must be grouped in place, in one pass. Signal words: three groups, three classes, less, equal and greater, around a pivot, colours, group but not sort.
Start: low and mid on the first index, high on the last.
Step: look at the value at mid. Front group: swap with low, move low and mid on. Middle group: move mid on. Back group: swap with high, move high back, and leave mid where it is.
Stop: when mid passes high. Loop while mid <= high, so the last unknown value is checked.
Around a pivot: less than the pivot is the front group, equal is the middle and greater is the back. This is the partition step of quickselect and of quicksort with many repeats.
It does not keep equal values in their input order. If order matters, use a second array.
Cost: each step shrinks the unknown region by one, so exactly n steps: O(n) time, one pass, O(1) extra space.
def dutch_flag(values):
# [1] Start: low and mid at the front, high at the back. Every value is unknown.
low = 0
mid = 0
high = len(values) - 1
# [2] While the unknown region mid..high is not empty, check the value at mid.
while mid <= high:
# [3] A 0: swap it to the end of the 0 block. The value from low is already checked, so both move on.
if values[mid] == 0:
values[low], values[mid] = values[mid], values[low]
low += 1
mid += 1
# [4] A 1: it already sits at the end of the 1 block, so mid moves on.
elif values[mid] == 1:
mid += 1
# [5] A 2: swap it to the front of the 2 block. The value from high is unchecked, so mid stays.
else:
values[mid], values[high] = values[high], values[mid]
high -= 1
# [6] The unknown region is empty: all 0s, then all 1s, then all 2s.
return valuespublic int[] dutchFlag(int[] values) {
// [1] Start: low and mid at the front, high at the back. Every value is unknown.
int low = 0;
int mid = 0;
int high = values.length - 1;
// [2] While the unknown region mid..high is not empty, check the value at mid.
while (mid <= high) {
// [3] A 0: swap it to the end of the 0 block. The value from low is already checked, so both move on.
if (values[mid] == 0) {
int moved = values[low];
values[low] = values[mid];
values[mid] = moved;
low++;
mid++;
}
// [4] A 1: it already sits at the end of the 1 block, so mid moves on.
else if (values[mid] == 1) {
mid++;
}
// [5] A 2: swap it to the front of the 2 block. The value from high is unchecked, so mid stays.
else {
int moved = values[high];
values[high] = values[mid];
values[mid] = moved;
high--;
}
}
// [6] The unknown region is empty: all 0s, then all 1s, then all 2s.
return values;
}vector<int> dutchFlag(vector<int>& values) {
// [1] Start: low and mid at the front, high at the back. Every value is unknown.
int low = 0;
int mid = 0;
int high = (int) values.size() - 1;
// [2] While the unknown region mid..high is not empty, check the value at mid.
while (mid <= high) {
// [3] A 0: swap it to the end of the 0 block. The value from low is already checked, so both move on.
if (values[mid] == 0) {
swap(values[low], values[mid]);
low++;
mid++;
}
// [4] A 1: it already sits at the end of the 1 block, so mid moves on.
else if (values[mid] == 1) {
mid++;
}
// [5] A 2: swap it to the front of the 2 block. The value from high is unchecked, so mid stays.
else {
swap(values[mid], values[high]);
high--;
}
}
// [6] The unknown region is empty: all 0s, then all 1s, then all 2s.
return values;
}function dutchFlag(values) {
// [1] Start: low and mid at the front, high at the back. Every value is unknown.
let low = 0;
let mid = 0;
let high = values.length - 1;
// [2] While the unknown region mid..high is not empty, check the value at mid.
while (mid <= high) {
// [3] A 0: swap it to the end of the 0 block. The value from low is already checked, so both move on.
if (values[mid] === 0) {
[values[low], values[mid]] = [values[mid], values[low]];
low++;
mid++;
}
// [4] A 1: it already sits at the end of the 1 block, so mid moves on.
else if (values[mid] === 1) {
mid++;
}
// [5] A 2: swap it to the front of the 2 block. The value from high is unchecked, so mid stays.
else {
[values[mid], values[high]] = [values[high], values[mid]];
high--;
}
}
// [6] The unknown region is empty: all 0s, then all 1s, then all 2s.
return values;
}def partition_around(values, pivot):
# [1] Start: low and mid at the front, high at the back. Every value is unknown.
low = 0
mid = 0
high = len(values) - 1
# [2] While the unknown region mid..high is not empty, compare the value at mid with the pivot.
while mid <= high:
# [3] Less: swap it to the end of the less block. The value from low is already checked, so both move on.
if values[mid] < pivot:
values[low], values[mid] = values[mid], values[low]
low += 1
mid += 1
# [4] Equal: it already sits at the end of the equal block, so mid moves on.
elif values[mid] == pivot:
mid += 1
# [5] Greater: swap it to the front of the greater block. The value from high is unchecked, so mid stays.
else:
values[mid], values[high] = values[high], values[mid]
high -= 1
# [6] The unknown region is empty: less, then equal, then greater.
return valuespublic int[] partitionAround(int[] values, int pivot) {
// [1] Start: low and mid at the front, high at the back. Every value is unknown.
int low = 0;
int mid = 0;
int high = values.length - 1;
// [2] While the unknown region mid..high is not empty, compare the value at mid with the pivot.
while (mid <= high) {
// [3] Less: swap it to the end of the less block. The value from low is already checked, so both move on.
if (values[mid] < pivot) {
int moved = values[low];
values[low] = values[mid];
values[mid] = moved;
low++;
mid++;
}
// [4] Equal: it already sits at the end of the equal block, so mid moves on.
else if (values[mid] == pivot) {
mid++;
}
// [5] Greater: swap it to the front of the greater block. The value from high is unchecked, so mid stays.
else {
int moved = values[high];
values[high] = values[mid];
values[mid] = moved;
high--;
}
}
// [6] The unknown region is empty: less, then equal, then greater.
return values;
}vector<int> partitionAround(vector<int>& values, int pivot) {
// [1] Start: low and mid at the front, high at the back. Every value is unknown.
int low = 0;
int mid = 0;
int high = (int) values.size() - 1;
// [2] While the unknown region mid..high is not empty, compare the value at mid with the pivot.
while (mid <= high) {
// [3] Less: swap it to the end of the less block. The value from low is already checked, so both move on.
if (values[mid] < pivot) {
swap(values[low], values[mid]);
low++;
mid++;
}
// [4] Equal: it already sits at the end of the equal block, so mid moves on.
else if (values[mid] == pivot) {
mid++;
}
// [5] Greater: swap it to the front of the greater block. The value from high is unchecked, so mid stays.
else {
swap(values[mid], values[high]);
high--;
}
}
// [6] The unknown region is empty: less, then equal, then greater.
return values;
}function partitionAround(values, pivot) {
// [1] Start: low and mid at the front, high at the back. Every value is unknown.
let low = 0;
let mid = 0;
let high = values.length - 1;
// [2] While the unknown region mid..high is not empty, compare the value at mid with the pivot.
while (mid <= high) {
// [3] Less: swap it to the end of the less block. The value from low is already checked, so both move on.
if (values[mid] < pivot) {
[values[low], values[mid]] = [values[mid], values[low]];
low++;
mid++;
}
// [4] Equal: it already sits at the end of the equal block, so mid moves on.
else if (values[mid] === pivot) {
mid++;
}
// [5] Greater: swap it to the front of the greater block. The value from high is unchecked, so mid stays.
else {
[values[mid], values[high]] = [values[high], values[mid]];
high--;
}
}
// [6] The unknown region is empty: less, then equal, then greater.
return values;
}else:
values[mid], values[high] = values[high], values[mid]
high -= 1
mid += 1 # bug: the value that came back from high has not been checkedThis version loops while mid < high.
Each parcel also has a code, and the loading team wants parcels of one class to stay in arrival order. You run the flag on the classes and carries each code along.
Your team lead brings a request from the zone planners. Parcels for five delivery zones share one belt, with zones 0 to 4: [3, 0, 4, 1, 3, 2, 0]. They want zone 0 first and zone 4 last.
systemdesignmastery.dev/dsa/cheatsheets/three-way-partition