🧠
Optimal One-Pass Dutch National Flag Algorithm
💡 This is the classic optimal solution that sorts the array in one pass using three pointers, minimizing operations and space.
Intuition
Use three pointers to partition the array into four sections: all 0s before low, all 1s between low and mid, unknown between mid and high, and all 2s after high.
Algorithm
- Initialize three pointers: low = 0, mid = 0, high = n-1.
- Traverse the array with mid pointer until it passes high.
- If nums[mid] == 0, swap with nums[low], increment low and mid.
- If nums[mid] == 1, just increment mid.
- If nums[mid] == 2, swap with nums[high], decrement high.
- Continue until mid > high.
💡 This approach is tricky at first but ensures all colors are sorted in one pass without extra space.
def sortColors(nums):
low, mid, high = 0, 0, len(nums) - 1
while mid <= high:
if nums[mid] == 0:
nums[low], nums[mid] = nums[mid], nums[low]
low += 1
mid += 1
elif nums[mid] == 1:
mid += 1
else: # nums[mid] == 2
nums[mid], nums[high] = nums[high], nums[mid]
high -= 1
# Driver code
if __name__ == '__main__':
arr = [2,0,2,1,1,0]
sortColors(arr)
print(arr)
Line Notes
low, mid, high = 0, 0, len(nums) - 1Initialize pointers for partitioning the array into zones
while mid <= high:Process elements until mid pointer passes high pointer
if nums[mid] == 0:If current element is 0, swap it to the front zone
nums[low], nums[mid] = nums[mid], nums[low]Swap 0 to the low pointer position
low += 1Move low pointer forward as 0 zone grows
mid += 1Move mid pointer forward
elif nums[mid] == 1:If current element is 1, it's in correct middle zone, just move mid
else: # nums[mid] == 2If current element is 2, swap it to the end zone
nums[mid], nums[high] = nums[high], nums[mid]Swap 2 to the high pointer position
high -= 1Move high pointer backward as 2 zone grows
import java.util.Arrays;
public class SortColors {
public static void sortColors(int[] nums) {
int low = 0, mid = 0, high = nums.length - 1;
while (mid <= high) {
if (nums[mid] == 0) {
int temp = nums[low];
nums[low] = nums[mid];
nums[mid] = temp;
low++;
mid++;
} else if (nums[mid] == 1) {
mid++;
} else {
int temp = nums[mid];
nums[mid] = nums[high];
nums[high] = temp;
high--;
}
}
}
public static void main(String[] args) {
int[] arr = {2,0,2,1,1,0};
sortColors(arr);
System.out.println(Arrays.toString(arr));
}
}
Line Notes
int low = 0, mid = 0, high = nums.length - 1;Initialize pointers for partitioning the array
while (mid <= high)Process elements until mid passes high
if (nums[mid] == 0)If current element is 0, swap to front zone
int temp = nums[low];Swap operation part 1
nums[low] = nums[mid];Swap operation part 2
nums[mid] = temp;Swap operation part 3
low++;Move low pointer forward
mid++;Move mid pointer forward
else if (nums[mid] == 1)If current element is 1, just move mid
elseIf current element is 2, swap to end zone
int temp = nums[mid];Swap operation part 1
nums[mid] = nums[high];Swap operation part 2
nums[high] = temp;Swap operation part 3
high--;Move high pointer backward
#include <iostream>
#include <vector>
using namespace std;
void sortColors(vector<int>& nums) {
int low = 0, mid = 0, high = nums.size() - 1;
while (mid <= high) {
if (nums[mid] == 0) {
swap(nums[low], nums[mid]);
low++;
mid++;
} else if (nums[mid] == 1) {
mid++;
} else {
swap(nums[mid], nums[high]);
high--;
}
}
}
int main() {
vector<int> arr = {2,0,2,1,1,0};
sortColors(arr);
for (int num : arr) cout << num << ' ';
cout << endl;
return 0;
}
Line Notes
int low = 0, mid = 0, high = nums.size() - 1;Initialize pointers for partitioning the array
while (mid <= high)Process elements until mid passes high
if (nums[mid] == 0)If current element is 0, swap to front zone
swap(nums[low], nums[mid]);Swap 0 to low pointer position
low++;Move low pointer forward
mid++;Move mid pointer forward
else if (nums[mid] == 1)If current element is 1, just move mid
elseIf current element is 2, swap to end zone
swap(nums[mid], nums[high]);Swap 2 to high pointer position
high--;Move high pointer backward
function sortColors(nums) {
let low = 0, mid = 0, high = nums.length - 1;
while (mid <= high) {
if (nums[mid] === 0) {
[nums[low], nums[mid]] = [nums[mid], nums[low]];
low++;
mid++;
} else if (nums[mid] === 1) {
mid++;
} else {
[nums[mid], nums[high]] = [nums[high], nums[mid]];
high--;
}
}
}
// Test
const arr = [2,0,2,1,1,0];
sortColors(arr);
console.log(arr);
Line Notes
let low = 0, mid = 0, high = nums.length - 1;Initialize pointers for partitioning the array
while (mid <= high)Process elements until mid passes high
if (nums[mid] === 0)If current element is 0, swap to front zone
[nums[low], nums[mid]] = [nums[mid], nums[low]];Swap 0 to low pointer position
low++;Move low pointer forward
mid++;Move mid pointer forward
else if (nums[mid] === 1)If current element is 1, just move mid
elseIf current element is 2, swap to end zone
[nums[mid], nums[high]] = [nums[high], nums[mid]];Swap 2 to high pointer position
high--;Move high pointer backward
Single pass through the array with constant extra space using three pointers.
💡 For n=100,000, this means about 100,000 operations, the most efficient approach.
Interview Verdict: Accepted and optimal
This is the best approach to implement in interviews due to its efficiency and elegance.