Skip to content

Commit a9f9899

Browse files
Merge pull request #417 from guptushar27/patch-3
Create RadixSort.java
2 parents 04bef01 + 01bdc43 commit a9f9899

1 file changed

Lines changed: 139 additions & 0 deletions

File tree

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
File path (suggested): algorithms/sorting/RadixSort.java
3+
(If you want to use a package, uncomment the package line below and place the file accordingly)
4+
*/
5+
// package algorithms.sorting;
6+
7+
import java.util.Arrays;
8+
9+
/**
10+
* Algorithm Name : Radix Sort
11+
* Programming Language: Java
12+
* Category: Sorting
13+
* Difficulty Level: Medium (Intermediate)
14+
*
15+
* Problem Statement:
16+
* Implement Radix Sort (LSD - least significant digit first) to sort an array
17+
* of non-negative integers. Use Counting Sort as a stable subroutine to sort
18+
* based on individual digit positions.
19+
*
20+
* Requirements:
21+
* - Input: array of non-negative integers
22+
* - Output: sorted array in ascending order
23+
*
24+
* Complexities:
25+
* - Time Complexity (Worst/Best/Average): O(d * (n + b))
26+
* where d = number of digits in the maximum element,
27+
* n = number of elements,
28+
* b = base (10 for decimal)
29+
* - Space Complexity: O(n + b)
30+
*
31+
* Notes:
32+
* - This implementation assumes non-negative integers. To handle negative numbers,
33+
* separate negatives and positives, radix-sort their absolute values, then merge.
34+
*/
35+
public class RadixSort {
36+
37+
/**
38+
* Utility to get maximum value in arr[] — used to determine how many digits.
39+
*/
40+
private static int getMax(int[] arr) {
41+
if (arr == null || arr.length == 0) return 0;
42+
int max = arr[0];
43+
for (int v : arr) {
44+
if (v > max) max = v;
45+
}
46+
return max;
47+
}
48+
49+
/**
50+
* A stable Counting Sort that sorts arr[] according to the digit represented by exp.
51+
* exp = 1 -> sort by 1's digit
52+
* exp = 10 -> sort by 10's digit
53+
* exp = 100 -> sort by 100's digit, etc.
54+
*
55+
* Complexity of this subroutine: O(n + b) where b = 10 (digits 0..9)
56+
*/
57+
private static void countingSortByDigit(int[] arr, int exp) {
58+
int n = arr.length;
59+
if (n == 0) return;
60+
61+
int[] output = new int[n]; // output array to store sorted order by current digit
62+
int[] count = new int[10]; // count array for digits 0..9 (base 10)
63+
64+
// 1) Count occurrences of each digit in the given exp place
65+
for (int i = 0; i < n; i++) {
66+
int digit = (arr[i] / exp) % 10;
67+
count[digit]++;
68+
}
69+
70+
// 2) Convert count[] so that it contains actual positions (prefix sums)
71+
for (int i = 1; i < 10; i++) {
72+
count[i] += count[i - 1];
73+
}
74+
75+
// 3) Build the output array from right to left to maintain stability
76+
// (stable because elements with same digit appear in original relative order)
77+
for (int i = n - 1; i >= 0; i--) {
78+
int digit = (arr[i] / exp) % 10;
79+
int pos = count[digit] - 1; // index in output[]
80+
output[pos] = arr[i];
81+
count[digit]--; // decrease count for this digit
82+
}
83+
84+
// 4) Copy the output array back to arr[], so arr[] now contains numbers
85+
// sorted by current digit (exp)
86+
System.arraycopy(output, 0, arr, 0, n);
87+
}
88+
89+
/**
90+
* Main Radix Sort routine — sorts the array using digit-by-digit counting sort.
91+
*/
92+
public static void radixSort(int[] arr) {
93+
if (arr == null || arr.length <= 1) return; // already sorted or nothing to do
94+
95+
// Find the maximum number to know the number of digits
96+
int max = getMax(arr);
97+
98+
// Do counting sort for every digit. Note that instead of passing the digit number,
99+
// exp is passed. exp is 10^i where i is current digit number (0 -> LSD).
100+
for (int exp = 1; max / exp > 0; exp *= 10) {
101+
countingSortByDigit(arr, exp);
102+
}
103+
}
104+
105+
/** Utility to print array in a readable form. */
106+
private static void printArray(String label, int[] arr) {
107+
System.out.println(label + Arrays.toString(arr));
108+
}
109+
110+
/** Sample test cases covering: small arrays, repeated numbers, single-element array */
111+
public static void main(String[] args) {
112+
// Test case 1: Small array (classic example)
113+
int[] arr1 = {170, 45, 75, 90, 802, 24, 2, 66};
114+
printArray("Original arr1: ", arr1);
115+
radixSort(arr1);
116+
printArray("Sorted arr1: ", arr1);
117+
System.out.println();
118+
119+
// Test case 2: Array with repeated numbers
120+
int[] arr2 = {5, 3, 5, 2, 8, 5, 3};
121+
printArray("Original arr2: ", arr2);
122+
radixSort(arr2);
123+
printArray("Sorted arr2: ", arr2);
124+
System.out.println();
125+
126+
// Test case 3: Single-element array
127+
int[] arr3 = {42};
128+
printArray("Original arr3: ", arr3);
129+
radixSort(arr3);
130+
printArray("Sorted arr3: ", arr3);
131+
System.out.println();
132+
133+
// Optional: Empty array test
134+
int[] arr4 = {};
135+
printArray("Original arr4: ", arr4);
136+
radixSort(arr4);
137+
printArray("Sorted arr4: ", arr4);
138+
}
139+
}

0 commit comments

Comments
 (0)