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. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. int[]data = {1,2,-9,-134,12,1};
  13. int[]result = sort(data);
  14. System.out.println(Arrays.toString(result));
  15. }
  16. static int MAX_DIGIT_INT = 10; // MAX_INT = (2^31 - 1)
  17. static int[] sort(int[]data){
  18. ArrayList<Integer>[]count = new ArrayList[MAX_DIGIT_INT];
  19. for(int i = 0; i < MAX_DIGIT_INT; i++){
  20. count[i] = new ArrayList<>();
  21. }
  22. for(int i : data){
  23. count[getNumDigit(i)].add(i);
  24. }
  25. int[]result = new int[data.length];
  26. int index = 0;
  27. for(ArrayList<Integer> list : count){
  28. for(int i : list){
  29. result[index++] = i;
  30. }
  31. }
  32. return result;
  33. }
  34.  
  35. static int getNumDigit(int v){
  36. int result = 0;
  37. while(v != 0){
  38. result++;
  39. v /= 10;
  40. }
  41. return result;
  42. }
  43. }
Success #stdin #stdout 0.06s 32256KB
stdin
Standard input is empty
stdout
[1, 2, -9, 1, 12, -134]