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
kambledhruva14 hours ago
What is wrong with this code???
it shows runtime error.
Segmentation Fault (SIGSEGV)
Learn More aboutSeg Fault
----------------------------------------
void sort012(int a[], int n){
   int s= 0, e= n-1;
     while(s<=e){                if(a[s]==0){           s++;       }              if(a[s]>a[e]){                      int temp= a[s];           a[s] = a[e];           a[e] = temp;           // swap(a[s],a[e]);           s++;                  }       else           e++;            }   }
+2
prajapatiaakash3641 day ago
This Solution is best It take Time Complexity O(n) Without Extra Space 
Plz Upvote If like Solution  
 
       int low=0;       int mid=0;       int high=n-1;              while(mid<=high)       {           if(a[mid]==0){               swap(a[low],a[mid]);               low++;               mid++;           }           else if(a[mid]==1)           mid++;           else{               swap(a[mid],a[high]);               high--;           }       }
 
#Logic 
          Initially low, mid are set at 0 and high is at n-1
          Now, we iterate mid from 0 to high, and for every element
                 if it is equal to 0, we swap it with element at low, and increment low and mid
                 else if it is equal to 2, we swap it with element at high, and decrement high
                 else we just increment mid (i.e element is equal to 1)
0
dhanakdeepak422 days ago
 Arrays.sort(a);
0
shlokdayma662 days ago
res0 = []       res1 = []       res2 = []       for i in range(n):           if arr[i] == 0:               res0.append(arr[i])           elif arr[i] == 1:               res1.append(arr[i])           else:               res2.append(arr[i])       return res0+res1+res2 
 
why this code fails in test case with 65k inputs and works well in jnotebook
+3
vaibsu2 days ago
void sort012(int a[], int n)
{
    int mp[] = {0, 0, 0}; // counts of 0, 1, 2
    for(int i=0; i<n; i++){
        mp[a[i]]++;
    }
    int writeIndex = 0;
    for(int i = 0; i < 3; i++){ // 0, 1, 2
        for(int j = 0; j < mp[i]; j++){ // 0...count
            a[writeIndex++] = i;
        }
    }
}
-2
rohitraj31313 days ago
void sort012(int a[], int n)
{

sort(a, a + n);
   
   for(int i = 0; i < n; i++){
       cout << a[i] << " ";
}
+1
aligrewal213 days ago
int zeros=0,ones=0,two=0;
    for(int i=0; i<n; i++)
    {
        if(a[i]==0) zeros++;
        else if(a[i]==1) ones++;
        else two++;
    }
    for(int i=0; i<n; i++)
    {
        if(zeros) { a[i] = 0; zeros--; }
        else if(ones) { a[i] = 1; ones--; }
        else a[i] = 2;
    }
-1
19r11agd1m3 days ago
public static void sort012(int a[], int n)   {       // code here               Arrays.sort(a);   }}
0
abhishekdubey953 days ago
this is the most simple solution. Just count 0,1,&2 and put it in the array. Please upvote. public static void sort012(int a[], int n)   {       // code here       int count0 = 0;       int count1 = 0;       int count2 = 0;
       for(int i=0; i<a.length; i++){           if(a[i] == 0){               count0++;           }           else if(a[i] == 1){               count1++;           }           else{               count2++;           }       }
           int k = 0;           while(k < count0){               a[k] = 0;               k++;           }
           int l = k;           while(l < count0+count1){               a[l] = 1;               l++;           }                      int m = l;           while (m<a.length){               a[m] = 2;               m++;           }              }}
+1
adamyasharma4933 days ago
void sort012(int arr[], int n)
    {
        // code here 
        
        // O(n) solution
        int low=0,mid=0,high=n-1;
        while(mid<=high)
        {
            if(arr[mid]==1)mid++;
            else if(arr[mid]==2)
            {
                swap(arr[high],arr[mid]);
                high--;
            }
            else //0s
            {
                swap(arr[low],arr[mid]);
                low++;
                mid++;
            }
        }
        return ;
    }
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.