fork download
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class countingSortWithRange{
  5. public static void main(String[] arg) throws IOException{
  6.  
  7. int len = Integer.parseInt(br.readLine());
  8. int m = Integer.parseInt(br.readLine());
  9. int n = Integer.parseInt(br.readLine());
  10.  
  11. String st[] = br.readLine().split(" ");
  12.  
  13. int arr[] = new int[len];
  14.  
  15. int i;
  16.  
  17. for(i = 0; i < len; i++){
  18. arr[i] = Integer.parseInt(st[i]);
  19. }
  20.  
  21. countSort(arr, m, n);
  22. }
  23.  
  24. static void countSort(int arr[], int m, int n){
  25. int len = arr.length;
  26.  
  27. int i;
  28.  
  29. int range = n-m+1;
  30.  
  31. int count[] = new int[range+1];
  32.  
  33. for(i = 0 ; i < len; i++){
  34. count[arr[i]-m]++;
  35. }
  36.  
  37. for(i = 1; i < count.length; i++){
  38. count[i] += count[i-1];
  39. }
  40.  
  41. int result[] = new int[len];
  42.  
  43. for(i = len-1; i >= 0; i--){
  44. result[count[arr[i]-m]-1] = arr[i];
  45. count[arr[i]-m]--;
  46. }
  47.  
  48. for(i = 0 ; i <len; i++){
  49. System.out.print(result[i] + " ");
  50. }
  51. }
  52. }
Success #stdin #stdout 0.05s 4386816KB
stdin
10
5
25
20 5 14 16 24 20 10 8 12 19
stdout
5 8 10 12 14 16 19 20 20 24