Sort an array of 0s, 1s and 2s | Dutch National Flag problem
Given an array of size N, that contains only 0s, 1s, and 2s. Our task is to sort the array in ascending order, so that all the 0 comes first, followed by the 1s, and then at the end all the 2s. Since we are told to sort so you might think of using any sorting algorithm, or to achieve the best time complexity you might think of using heap sort or merge sort.
This problem is also similar to the Dutch National Flag problem proposed by Edsger Dijkstra. The problem says.
Given N balls of colour red, white or blue arranged in a line in random order. You have to arrange all the balls such that the balls with the same colours are adjacent with the order of the balls, with the order of the colours being red, white and blue (i.e., all red coloured balls come first then the white coloured balls and then the blue coloured balls).
But can we do it any better? maybe try to achieve O(n) time complexity if possible. Give it a shot before we discuss the solution.
Approach:
Since we can see that there are only three types of values. 0 1 and 2 so we can just do a simple thing. We can count how many numbers of 0 1 and 2 are there for this we can simply create three counter variables to store the count of the three numbers. Then what we are going to do is. We can store 0s from the first till the number of zeros present(which we already know since now we have the count), then after that index, we can start filling the 1s in the array and then the 2s respectively. since we are overriding the values so we don't need to do any sorting. I hope you got the logic, if it was any difficult, I hope it wasn't, you will be able to get it after you see the code.
Code:
Here I have only shown the method, the main method is excluded.
//User function template for Java
class Solution
{
public static void sort012(int a[], int n)
{
// code here
int zero=0,one=0,two=0;// counter variables
for(int i=0;i<n;i++){
if(a[i]==0)zero++;
if(a[i]==1)one++;
if(a[i]==2)two++;
}
for(int i=0;i<zero;i++){
a[i]=0;
}
for(int i=zero;i<one+zero;i++){
a[i]=1;
}
for(int i=one+zero;i<n;i++){
a[i]=2;
}
}
}
Let's break down how the code works step by step:
The function
sort012(int a[], int n)
takes an integer arraya[]
and its lengthn
as input parameters.Three counter variables
zero
,one
, andtwo
are initialized to keep track of the counts of each element (0s, 1s, and 2s) in the array.The first loop iterates through the array to count the occurrences of 0s, 1s, and 2s. For each element, it checks its value and increments the corresponding counter variable accordingly.
After counting the elements, the next three loops are used to overwrite the original array with the sorted values. Here's how it's done:
The first loop runs
zero
times and sets the firstzero
elements in the array to 0.The second loop runs
one
times and sets the nextone
elements in the array to 1.The third loop runs
two
times and sets the remaining elements in the array (starting from indexone+zero
) to 2.
At the end of these loops, the array
a[]
is sorted in ascending order with all the 0s placed at the beginning, followed by the 1s, and finally the 2s.
The key idea here is to count the occurrences of each element and then use that count to place the elements in the correct positions in the array. This approach ensures that the array gets sorted without using any traditional sorting algorithms like quicksort or mergesort. I have also made a video on the same, if you preffer that I have put it here for you.
I hope you got the approach and the solution, do try this on your own in leetcode. If you had any problem understanding it, go through the blog once again and then you can reach out to me through my socials, I'll be more than happy to hear from you. If you have any feedback or tips you can send that to me as well. So see you next time. Till then keep coding and keep learning Thankyou.