Given an array which consists of only 0, 1 and 2 sort the array without using any sorting algorithm in Java

Sort the array with elements 0, 1 and 2 in Java

Here, in this page we will discuss the program to sort the array with elements 0, 1 and 2 in Java . We use the concept of counting the frequency of 0, 1 and 2 . We are giving with the size of the array along with array elements .We have to print sorted array

Sort the array with elements

Algorithm :

  • Declare three variable count_0, count_1 and count_2

  • Using count function, count the number of zero in array and store in count_0

  • Using count function, count the number of one in array and store in count_1

  • Using count function, count the number of two in array and store in count_2

  • Declare an array name new_arr

  • Append all the 0 to new_arr

  • Append all the 1 to new_arr

  • Append all the 2 to new_arr

  • Print new_arr

Sort the array consisting of 0,1 and 2 using java

Java code

Run

import java.util.*;
public
class Main {
    public
    static void arrange012(int[] arr) {  // we arrange 0 1 2 iteratively
        int i = 0, j = 0, k = arr.length - 1;
        // 0 to j-1  ==> All Zeroes

        while (i <= k) {
            if (arr[i] == 0) {
                swap(arr, i, j);
                i++;
                j++;
            } else if (arr[i] == 1) {
                i++;
            } else {
                swap(arr, i, k);
                k--;
            }
        }
    }
    // used for swapping ith and jth elements of array
    public
    static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
    // main function
    public
    static void main(String[] args) throws Exception {
        Scanner scn = new Scanner(System.in);
        
        int n = 4;
        int[] arr = new int[]{ 1,0,0,1}; 
    
        arrange012(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

Output

0
0
1
1

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

5 comments on “Given an array which consists of only 0, 1 and 2 sort the array without using any sorting algorithm in Java”


  • Varun Patil

    import java.util.ArrayList;

    public class ArraySort012 {
    public static void main(String[] args) {
    int arr[] = { 0, 1, 2, 2, 1, 0, 0, 1, 2, 2, 1, 1, 1, 1 };
    int zero = 0;
    int one = 0;
    int two = 0;
    for (int i = 0; i < arr.length; i++) {
    if (arr[i] == 0)
    zero++;
    else if (arr[i] == 1)
    one++;
    else if (arr[i] == 2)
    two++;
    }
    ArrayList arrl = new ArrayList();

    for (int i = 0; i < zero; i++) {
    arrl.add(i, 0);
    }
    for (int j = zero; j < zero + one; j++) {
    arrl.add(j, 1);
    }
    for (int j = zero + one; j < zero + one + two; j++) {
    arrl.add(j, 2);
    }

    System.out.println(arrl);
    }
    }


  • Abhishek

    import java.util.Arrays;

    public class WithoutSortAlgo{

    public static void main(String[] args) {
    int[] arr = {0,1,0,0,0,1,2,2,2,1,1,0,0,0,0,2,1,0,1,2};

    int count_0 = 0;
    int count_1 = 0;
    int count_2 = 0;

    for (int i = 0; i < arr.length; i++) {
    if (arr[i] == 0) {
    count_0++;
    }else if (arr[i] == 1){
    count_1++;
    }else{
    count_2++;
    }
    }
    int newCount_1 = count_0;
    int newCount_2 = count_0 + count_1;
    int[] newArr = new int[arr.length];
    for (int i = 0; i < count_0; i++) {
    newArr[i] = 0;
    }
    for (int i = newCount_1; i < newCount_2; i++) {
    newArr[i] = 1;
    }
    for (int i = newCount_2; i < newArr.length; i++) {
    newArr[i] = 2;
    }

    System.out.println(Arrays.toString(newArr));
    }
    }


  • NAZIA KHAN

    public static void main(String[] args) {
    int[] a={1,0,2,1,1,0,2,1,0};
    int b0=0,b1=0,b2=0;
    for(int i=0;i<a.length;i++){
    if(a[i]==0) b0++;
    else if(a[i]==1) b1++;
    else{
    b2++;
    }
    }
    for(int i=0;i<b0;i++){
    System.out.print("0"+" ");
    }
    for(int i=0;i<b1;i++){
    System.out.print("1"+" ");
    }
    for(int i=0;i<b2;i++){
    System.out.print("2"+" ");
    }
    }


  • Gyanendra

    import java.util.*;
    public class Main{
    public static void main(String[] args){
    Scanner scn=new Scanner(System.in);
    int n=scn.nextInt();
    int[] arr=new int[n];

    for(int i=0;i<n;i++){
    arr[i]=scn.nextInt();
    }

    int i=0,j=0,k=arr.length-1;

    while(j<=k){

    if(arr[j]==0){
    swap(arr,i,j);
    i++;
    j++;
    }
    else if(arr[j]==2){
    swap(arr,j,k);
    k–;
    }
    else{
    j++;
    }
    }

    for(int val:arr){
    System.out.print(val+" ");
    }
    }

    public static void swap(int[] arr,int i,int j){
    int temp=arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
    }
    }