-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadixSort.java
More file actions
42 lines (37 loc) · 1 KB
/
RadixSort.java
File metadata and controls
42 lines (37 loc) · 1 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
package sorting;
/**
* What if the elements are in range from 1 to n2? Radix Sort is the answer. In
* linear time we can solve this. Here we have assumed the base as 10. For
* linear time convert all the numbers into base n and then count sort them for
* linear complexity.
*
* Link: http://www.geeksforgeeks.org/radix-sort/
*
* @author shivam.maharshi
*/
public class RadixSort {
// For simplicity base is considered 10.
public static int[] sort(int[] arr, int n) {
int[][] a = convert(arr, n);
int digit = a[0].length;
// TODO:
return null;
}
public static int[][] convert(int[] arr, int n) {
int[][] a = new int[arr.length][(int) Math.log10(n)+1];
for (int i = 0; i < a.length; i++) {
int num = arr[i];
int j = a[0].length - 1;
while (num != 0) {
a[i][j] = num % 10;
num /= 10;
j--;
}
}
return a;
}
public static void main(String[] args) {
int[] arr = {170, 45, 75, 90, 802, 24, 2, 66};
convert(arr, 802);
}
}