|
| 1 | +import java.util.Arrays; |
| 2 | + |
| 3 | +public class RadixSort { |
| 4 | + |
| 5 | + /** |
| 6 | + * The main function that sorts arr[] using Radix Sort. |
| 7 | + */ |
| 8 | + public void sort(int[] arr) { |
| 9 | + if (arr == null || arr.length == 0) { |
| 10 | + return; |
| 11 | + } |
| 12 | + |
| 13 | + // 1. Find the maximum number to know the number of digits. |
| 14 | + int max = getMax(arr); |
| 15 | + |
| 16 | + // 2. Do counting sort for every digit. |
| 17 | + // We use 'exp' (exponent) to represent the current digit's place |
| 18 | + // (1, 10, 100, 1000...) |
| 19 | + for (int exp = 1; max / exp > 0; exp *= 10) { |
| 20 | + countingSortByDigit(arr, exp); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * A utility function to get the maximum value in arr[]. |
| 26 | + */ |
| 27 | + private int getMax(int[] arr) { |
| 28 | + int max = arr[0]; |
| 29 | + for (int i = 1; i < arr.length; i++) { |
| 30 | + if (arr[i] > max) { |
| 31 | + max = arr[i]; |
| 32 | + } |
| 33 | + } |
| 34 | + return max; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * A stable sort (Counting Sort) to sort array elements |
| 39 | + * based on the digit at the 'exp' place. |
| 40 | + */ |
| 41 | + private void countingSortByDigit(int[] arr, int exp) { |
| 42 | + int n = arr.length; |
| 43 | + int[] output = new int[n]; // The sorted output array |
| 44 | + int[] count = new int[10]; // To store count of digits (0-9) |
| 45 | + |
| 46 | + // Initialize count array to all zeros |
| 47 | + Arrays.fill(count, 0); |
| 48 | + |
| 49 | + // 1. Store count of occurrences of each digit |
| 50 | + for (int i = 0; i < n; i++) { |
| 51 | + // Get the digit at the 'exp' place |
| 52 | + int digit = (arr[i] / exp) % 10; |
| 53 | + count[digit]++; |
| 54 | + } |
| 55 | + |
| 56 | + // 2. Change count[i] so that it now contains the |
| 57 | + // actual position of this digit in output[] |
| 58 | + for (int i = 1; i < 10; i++) { |
| 59 | + count[i] += count[i - 1]; |
| 60 | + } |
| 61 | + |
| 62 | + // 3. Build the output array |
| 63 | + // We go in reverse order to make the sort stable |
| 64 | + for (int i = n - 1; i >= 0; i--) { |
| 65 | + int digit = (arr[i] / exp) % 10; |
| 66 | + |
| 67 | + // Place the element at its correct sorted position |
| 68 | + output[count[digit] - 1] = arr[i]; |
| 69 | + |
| 70 | + // Decrement the count for that digit |
| 71 | + count[digit]--; |
| 72 | + } |
| 73 | + |
| 74 | + // 4. Copy the sorted output array back to arr[] |
| 75 | + System.arraycopy(output, 0, arr, 0, n); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * A main method to test the sort. |
| 80 | + */ |
| 81 | + public static void main(String[] args) { |
| 82 | + int[] data = {170, 45, 75, 90, 802, 24, 2, 66}; |
| 83 | + |
| 84 | + System.out.println("Original array: " + Arrays.toString(data)); |
| 85 | + |
| 86 | + RadixSort sorter = new RadixSort(); |
| 87 | + sorter.sort(data); |
| 88 | + |
| 89 | + System.out.println("Sorted array: " + Arrays.toString(data)); |
| 90 | + } |
| 91 | +} |
0 commit comments