/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { private static void sort(int arr[]) { int length = arr.length; int zeroIndex = -1; int twoIndex = length; int currentIndex = 0; while(currentIndex<twoIndex){ if(arr[currentIndex] == 0){ zeroIndex++; if(arr[zeroIndex] == 1){ arr[currentIndex] = 1; } arr[zeroIndex] = 0; currentIndex++; }else if(arr[currentIndex] == 1){ currentIndex++; }else if(arr[currentIndex] == 2){ twoIndex--; arr[currentIndex] = arr[twoIndex]; arr[twoIndex] = 2; } print(arr, length); } } private static void print(int arr[], int length){ for(int i=0;i<length;i++){ } } { int arr[] = {0, 1, 2, 0, 1, 2}; sort(arr); print(arr, arr.length); } }
Standard input is empty
1:0, 1, 2, 0, 1, 2, 2:0, 1, 2, 0, 1, 2, 2:0, 1, 2, 0, 1, 2, 2:0, 1, 1, 0, 2, 2, 3:0, 1, 1, 0, 2, 2, 4:0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2,