Guest User

Untitled

a guest
Feb 25th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. private static void radixSort(int[] a) {
  2.  
  3. int maxOne = a[0], exp = 1, size = a.length;
  4.  
  5. for (int anA : a) if (anA > maxOne) maxOne = anA;//Get max
  6.  
  7. while (maxOne / exp > 0) {
  8.  
  9. int[] bucket = new int[10];
  10.  
  11. for (int num : a) {//get count of each digit
  12. int digit = (num / exp) % 10;
  13. bucket[digit] += 1;
  14. }
  15.  
  16. for (int i = 1; i <= 9; i++)//define range of
  17. bucket[i] += bucket[i - 1];//numbers with the same digit
  18.  
  19. int temp[] = new int[size];
  20.  
  21. for (int i = size - 1; i >= 0; i--) {
  22. int digit = (a[i] / exp) % 10;
  23. temp[--bucket[digit]] = a[i];//copy elements to
  24. } //temp array
  25.  
  26. for (int i = 0; i < size; i++) a[i] = temp[i];//copy temp to input array
  27.  
  28. exp *= 10;//go next digit
  29.  
  30. }
  31.  
  32. }
Add Comment
Please, Sign In to add comment