-
Notifications
You must be signed in to change notification settings - Fork 853
Expand file tree
/
Copy pathRadixSort.java
More file actions
56 lines (48 loc) · 1.59 KB
/
RadixSort.java
File metadata and controls
56 lines (48 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* RadixSort.java
*
* Description:
* Radix Sort is a non-comparison sorting algorithm that sorts numbers
* digit by digit starting from the least significant digit (LSD) to
* the most significant digit (MSD). It uses Counting Sort as a subroutine.
*
* Time Complexity: O(n * k) where k is the number of digits
* Space Complexity: O(n + k)
* Stable: Yes
*/
public class RadixSort {
public static void radixSort(int[] arr) {
if (arr.length == 0) return;
// Find the maximum number to know number of digits
int max = arr[0];
for (int num : arr) {
if (num > max) max = num;
}
// Apply counting sort for every digit (exp = 1, 10, 100, ...)
for (int exp = 1; max / exp > 0; exp *= 10) {
countingSortByDigit(arr, exp);
}
}
private static void countingSortByDigit(int[] arr, int exp) {
int n = arr.length;
int[] output = new int[n];
int[] count = new int[10]; // Digits 0–9
// Store count of occurrences for each digit
for (int num : arr) {
int index = (num / exp) % 10;
count[index]++;
}
// Cumulative count
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
// Build the output array (traverse backwards for stability)
for (int i = n - 1; i >= 0; i--) {
int index = (arr[i] / exp) % 10;
output[count[index] - 1] = arr[i];
count[index]--;
}
// Copy output to original array
System.arraycopy(output, 0, arr, 0, n);
}
}