|
| 1 | +import java.util.Arrays; |
| 2 | +import java.util.InputMismatchException; |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +public class RadixSort { |
| 6 | + |
| 7 | + // Function to find the maximum element in the array |
| 8 | + public static int getMax(int[] arr) { |
| 9 | + int max = arr[0]; |
| 10 | + for (int i = 1; i < arr.length; i++) { |
| 11 | + if (arr[i] > max) { |
| 12 | + max = arr[i]; |
| 13 | + } |
| 14 | + } |
| 15 | + return max; |
| 16 | + } |
| 17 | + |
| 18 | + // A function to do counting sort of arr[] according to the digit represented by exp |
| 19 | + public static void countSort(int[] arr, int exp) { |
| 20 | + int n = arr.length; |
| 21 | + int[] output = new int[n]; // output array |
| 22 | + int[] count = new int[10]; |
| 23 | + Arrays.fill(count, 0); |
| 24 | + |
| 25 | + // Store count of occurrences in count[] |
| 26 | + for (int i = 0; i < n; i++) { |
| 27 | + count[(arr[i] / exp) % 10]++; |
| 28 | + } |
| 29 | + |
| 30 | + // Change count[i] so that count[i] now contains actual position of this digit in output[] |
| 31 | + for (int i = 1; i < 10; i++) { |
| 32 | + count[i] += count[i - 1]; |
| 33 | + } |
| 34 | + |
| 35 | + // Build the output array |
| 36 | + for (int i = n - 1; i >= 0; i--) { |
| 37 | + output[count[(arr[i] / exp) % 10] - 1] = arr[i]; |
| 38 | + count[(arr[i] / exp) % 10]--; |
| 39 | + } |
| 40 | + |
| 41 | + // Copy the output array to arr[], so that arr[] now contains sorted numbers according to the current digit |
| 42 | + System.arraycopy(output, 0, arr, 0, n); |
| 43 | + } |
| 44 | + |
| 45 | + // The main function to that sorts arr[] of size n using Radix Sort |
| 46 | + public static void radixSort(int[] arr) { |
| 47 | + int max = getMax(arr); |
| 48 | + |
| 49 | + // Do counting sort for every digit. |
| 50 | + // Instead of passing digit number, exp is passed. exp is 10^i where i is the current digit number |
| 51 | + for (int exp = 1; max / exp > 0; exp *= 10) { |
| 52 | + countSort(arr, exp); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public static void main(String[] args) { |
| 57 | + Scanner scanner = new Scanner(System.in); |
| 58 | + |
| 59 | + try { |
| 60 | + System.out.print("Enter the number of elements: "); |
| 61 | + int n = scanner.nextInt(); |
| 62 | + if (n <= 0) { |
| 63 | + throw new IllegalArgumentException("Number of elements must be positive."); |
| 64 | + } |
| 65 | + |
| 66 | + int[] arr = new int[n]; |
| 67 | + |
| 68 | + System.out.println("Enter the elements:"); |
| 69 | + for (int i = 0; i < n; i++) { |
| 70 | + arr[i] = scanner.nextInt(); |
| 71 | + } |
| 72 | + |
| 73 | + System.out.println("Original array: " + Arrays.toString(arr)); |
| 74 | + |
| 75 | + radixSort(arr); |
| 76 | + |
| 77 | + System.out.println("Sorted array: " + Arrays.toString(arr)); |
| 78 | + } catch (InputMismatchException e) { |
| 79 | + System.out.println("Invalid input. Please enter integers only."); |
| 80 | + } catch (IllegalArgumentException e) { |
| 81 | + System.out.println(e.getMessage()); |
| 82 | + } finally { |
| 83 | + scanner.close(); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments