Sort 0s, 1s, and 2s using Dutch National Flag Algorithm
Definition:​
The Dutch National Flag Algorithm is an efficient sorting algorithm that sorts an array containing only 0's, 1's, and 2's in linear time. This algorithm uses a three-pointer approach and operates in-place, making it optimal in terms of both time and space.
Characteristics:​
-
Three Pointers:
The algorithm uses three pointers,low
,mid
, andhigh
:low
is used to place 0's.mid
is used to traverse the array.high
is used to place 2's.
-
In-Place Sorting:
The array is sorted in-place without using extra space, making the algorithm space-efficient. -
Efficient for Specific Inputs:
The algorithm is optimized for arrays with only three distinct values (0, 1, and 2), achieving linear time complexity.
Time Complexity:​
-
Best Case:
The array is traversed only once using themid
pointer, resulting in linear time complexity. -
Average Case:
The algorithm still runs in linear time even for a randomly arranged array, as each element is processed at most once. -
Worst Case:
In the worst case, the array may require multiple swaps, but the time complexity remains O(N), where N is the size of the array.
Space Complexity:​
- Space Complexity:
The algorithm uses constant extra space since no additional arrays or data structures are required.
Approach:​
The algorithm works by using the three pointers (low
, mid
, and high
) to partition the array into three sections:
nums[0]
tonums[low-1]
for 0's.nums[low]
tonums[mid-1]
for 1's.nums[high+1]
tonums[n-1]
for 2's.
Here are the steps involved:
- Initialize
low
andmid
at 0 andhigh
atsizeOfArray - 1
. - Iterate over the array with the
mid
pointer untilmid <= high
:- If
nums[mid] == 0
, swapnums[low]
andnums[mid]
, and increment bothlow
andmid
. - If
nums[mid] == 1
, simply incrementmid
. - If
nums[mid] == 2
, swapnums[mid]
andnums[high]
, and decrementhigh
without movingmid
.
- If
C++ Implementation:​
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Function to sort the array containing only 0s, 1s, and 2s
void sortZeroOneTwo(vector<int>& nums) {
// 3 pointers: low, mid, high
int low = 0, mid = 0, high = nums.size() - 1;
// Traverse the array using mid pointer
while (mid <= high) {
if (nums[mid] == 0) {
// Swap nums[low] and nums[mid], move both low and mid forward
swap(nums[low], nums[mid]);
low++;
mid++;
}
else if (nums[mid] == 1) {
// Move mid pointer forward
mid++;
}
else {
// Swap nums[mid] and nums[high], move high pointer backward
swap(nums[mid], nums[high]);
high--;
}
}
}
};
int main() {
vector<int> nums = {0, 2, 1, 2, 0, 1};
// Create an instance of Solution class
Solution sol;
// Sort the array
sol.sortZeroOneTwo(nums);
// Print the array elements after sorting
cout << "After sorting:" << endl;
for (int i = 0; i < nums.size(); i++) {
cout << nums[i] << " ";
}
cout << endl;
return 0;
}
Summary:​
The Dutch National Flag Algorithm efficiently sorts an array containing only 0's, 1's, and 2's in a single traversal of the array. It achieves optimal time complexity of and operates in-place, making it an ideal solution for this type of problem. The use of three pointers (low, mid, and high) ensures that the array is correctly partitioned, and the algorithm is both simple and effective.