Sort an array of 0s, 1s and 2s


Given an array A[] consisting 0s, 1s and 2s, write a function that sorts A[]. The functions should put all 0s first, then all 1s and all 2s in last.
Examples:

Input :  {0, 1, 2, 0, 1, 2}
Output : {0, 0, 1, 1, 2, 2}

Input :  {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1}
Output : {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2}



Code:

package microsoftInterviewPrep;

import java.util.Arrays;

public class SortArrayOf0_1_2 {
   
    static void sortArray(int[] a){
       
        int low = 0, high = a.length-1, mid = 0;
       
        while( mid <= high){
           
            if(a[mid] == 0){
                int temp = a[low];
                a[low] = a[mid];
                a[mid] = temp;
               
                low++;
                mid++;
            }
           
            else if(a[mid] == 2){
               
                int temp = a[high];
                a[high] = a[mid];
                a[mid] = temp;
                high--;
            }
           
            else{
                mid++;
            }
           
           
        }
       
        System.out.println(Arrays.toString(a));
       
       
    }
   
    public static void main(String[] args){
       
        int[] a =  {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1};
        sortArray(a);
       
    }

}



Output:

[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2]


Comments

Popular posts from this blog

Rearrange Array in Maximum-Minimum form

Second Largest Element

Check if a number is a power of another number