Sort an array of 0s, 1s and 2s | Practice | GeeksforGeeks

Given an array of size N containing only 0s, 1s, and 2s; sort the array in ascending order.

Example 1:
Input: 
N = 5
arr[]= {0 2 1 2 0}
Output:
0 0 1 2 2
Explanation:
0s 1s and 2s are segregated 
into ascending order.
Example 2:
Input: 
N = 3
arr[] = {0 1 0}
Output:
0 0 1
Explanation:
0s 1s and 2s are segregated 
into ascending order.

Your Task:
You don't need to read input or print anything. Your task is to complete the function sort012() that takes an array arr and N as input parameters and sorts the array in-place. 

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints:
1 <= N <= 10^6
0 <= A[i] <= 2
0
ashutosh998 hours ago
Can anyone tell why its failing for large array size.
 
def sort012(self,arr,n):       # code here       c = 0;       d = n-1;       for i in range(n-1):           if(arr[i]==0):               temp = arr[i];               arr[i]=arr[c];               arr[c]=temp;               c +=1;           if(arr[i]==2):               temp = arr[i];               arr[i]= arr[d];               arr[d]= temp;               d -= 1;       return arr; 
0
hunger4codes14 hours ago
class Solution
{
    public static void sort012(int a[], int n)
   {
      int lo=0,mid=0;int hi=n-1;
      while(mid<=hi){
          if(a[mid]==0){
              swap(a,mid,lo);
              lo++;mid++;
          }
          else if(a[mid]==1)
              mid++;
          else{
              swap(a,mid,hi);
              hi--;
          }
      }
   }
   private static void swap(int[] a, int mid, int lo) {
       int temp=a[lo];
       a[lo]=a[mid];
       a[mid]=temp;
   }
} 
-2
madhavendradey1 day ago
//Java Solution
 
class Solution{   public static void sort012(int a[], int n)   {       Arrays.sort(a);   }}
+1
98ankit471 day ago
int count0=0,count1=0,count2=0;       for(int i=0;i<n;i++)       {           if(a[i]==0)           {               ++count0;           }           if(a[i]==1)           {               ++count1;           }           if(a[i]==2)           {               ++count2;           }       }       for(int i=0;i<n;i++)       {           if(i<count0)           {               a[i]=0;           }           else if(i>=count0 && i<count0+count1)           {               a[i]=1;           }           else if(i>=count0+count1 &&i<count0+count1+count2)           {               a[i]=2;           }       }   }
0
sagarsgghule2 days ago
public static void sort012(int a[], int n)   {       // code here        int zero = 0;       int one = 0;              for(int i =0; i<n; i++)       {           if(a[i] == 0)               zero++;           else if(a[i] == 1)               one++;       }              for(int i=0; i<n; i++)       {           if(zero != 0)           {               a[i] = 0;               zero--;           }           else if(one != 0)           {               a[i] = 1;               one--;           }           else               a[i] = 2;       }   }
+1
rohitkumar010919972 days ago
void sort012(int a[], int n)   {       // code here        int count0=0, count1=0,count2=0;       for(int i=0;i<n;i++){           if(a[i]==0) count0++;           else if(a[i]==1) count1++;           else count2++;       }       for(int i=0; i<count0; i++){           a[i] = 0;       }       for(int i=count0; i<(count0+count1); i++){           a[i] = 1;       }       for(int i=(count0+count1); i<n; i++){           a[i]=2;       }   }
0
srijan786mohinesh3 days ago
plss koi batao what is problem in this  int x=-1;  int l=0;  for (int i = 0; i < 3; i++)  {      x++;      for (int j = 0; j < n; j++)      {          if (a[j]==x)          {              a[l]=x;              l++;          }                }             }
0
deepeshdhaundiyal4 days ago
       int count0 = 0;       int count1 = 0;       int count2 = 0;              for(int i =0; i<n; i++)       {           if(a[i] == 0)           {               count0++;           }           else if(a[i]==1)           {               count1++;           }           else if(a[i]==2)           {               count2++;           }       }               for(int i =0; i<n; i++)       {           if(count0 != 0)           {               a[i]=0;               count0--;           }           else if(count1 != 0)           {               a[i]=1;               count1--;           }           else if(count2 != 0)           {               a[i]=2;               count2--;           }       }
-2
kalaiabster4 days ago
JAVA SOLUTION :
 public static void sort012(int a[], int n)   {       Arrays.sort(a);   }
0
torq12845 days ago
 void sort012(int a[], int n)
    {
        // coode here 
        int l=0,m=0,h=n-1;
        while(m<=h)
        {
            if(a[m]==0)
            {
                swap(a[m],a[l]);
                l++;
                m++;
            }
            else if(a[m]==1)
              m++;
            else if(a[m]==2)
            {
               swap(a[m],a[h]);
               h--;
            }
        }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.