fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. private static void sort(int arr[]) {
  11. int length = arr.length;
  12. int zeroIndex = -1;
  13. int twoIndex = length;
  14. int currentIndex = 0;
  15. while(currentIndex<twoIndex){
  16. if(arr[currentIndex] == 0){
  17. zeroIndex++;
  18. if(arr[zeroIndex] == 1){
  19. arr[currentIndex] = 1;
  20. }
  21. arr[zeroIndex] = 0;
  22. currentIndex++;
  23. }else if(arr[currentIndex] == 1){
  24. currentIndex++;
  25. }else if(arr[currentIndex] == 2){
  26. twoIndex--;
  27. arr[currentIndex] = arr[twoIndex];
  28. arr[twoIndex] = 2;
  29. }
  30. System.out.print(currentIndex+":");
  31. print(arr, length);
  32. }
  33. }
  34. private static void print(int arr[], int length){
  35. for(int i=0;i<length;i++){
  36. System.out.print(arr[i]+", ");
  37. }
  38. System.out.println();
  39. }
  40. public static void main (String[] args) throws java.lang.Exception
  41. {
  42. int arr[] = {0, 1, 2, 0, 1, 2};
  43. sort(arr);
  44. print(arr, arr.length);
  45. }
  46. }
Success #stdin #stdout 0.05s 2184192KB
stdin
Standard input is empty
stdout
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,