Sorting 0s, 1s, and 2s with the Dutch National Flag Algorithm
Overview:​
The Dutch National Flag Algorithm is a highly efficient technique to sort an array containing only the elements 0, 1, and 2 in linear time. It utilizes a three-pointer strategy and performs sorting directly on the array, optimizing both time and space.
Key Features:​
-
Three Pointers Strategy:
The algorithm operates using three pointers (low,mid, andhigh), each having a specific role:lowfor positioning 0smidfor traversalhighfor positioning 2s
-
In-Place Sorting:
This approach does not require additional memory for sorting, making it space-efficient as the array is sorted in-place. -
Optimized for Specific Data:
Designed specifically for arrays with three distinct elements (0, 1, and 2), the algorithm runs in linear time, making it optimal for such cases.
Time Complexity:​
-
Best Case:
The array is traversed once using themidpointer, providing a linear runtime. -
Average Case:
Even in random input order, the algorithm processes each element a maximum of one time, maintaining linear time. -
Worst Case:
Although multiple swaps may be needed, the time complexity remains linear at , where is the array length.
Space Complexity:​
- Space Efficiency:
The algorithm uses constant extra space as it sorts the array in-place without requiring any auxiliary data structures.
Algorithm Steps:​
The algorithm follows these steps to partition the array using three pointers (low, mid, high):
- The range
nums[0]tonums[low-1]holds 0s. - The range
nums[low]tonums[mid-1]holds 1s. - The range
nums[high+1]tonums[n-1]holds 2s.
Process:
- Initialize
lowandmidat index 0 andhighat the last index of the array. - While
midis less than or equal tohigh, follow these rules:- If
nums[mid] == 0, swap it withnums[low], increment bothlowandmid. - If
nums[mid] == 1, incrementmidonly. - If
nums[mid] == 2, swap it withnums[high]and decrementhighwithout changingmid.
- If
C++ Code Implementation:​
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Function to sort an array with only 0s, 1s, and 2s
void sortZeroOneTwo(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> nums = {0, 2, 1, 2, 0, 1};
Solution sol;
sol.sortZeroOneTwo(nums);
cout << "After sorting: ";
for (int i = 0; i < nums.size(); i++) {
cout << nums[i] << " ";
}
cout << endl;
return 0;
}