1
- // Radix sort Java implementation
1
+ // Radix Sort implementation
2
2
3
3
import java .util .Arrays ;
4
4
5
- class RadixSort {
6
-
7
- // A utility function to get maximum value in arr[]
8
- static int getMax ( int arr [], int n )
9
- {
10
- int mx = arr [ 0 ] ;
11
- for ( int i = 1 ; i < n ; i ++)
12
- if ( arr [ i ] > mx )
13
- mx = arr [ i ];
14
- return mx ;
5
+ public class RadixSort {
6
+ private static final int BASE = 10 ;
7
+ public static void main ( String [] args ) {
8
+ int [] arr = new int []{ 1 , - 2 , - 456 , 803 , 1000 , - 2033 , - 9 , 0 , 10 };
9
+ long startTime = System . currentTimeMillis ();
10
+ System . out . println ( "Original Array: " + Arrays . toString ( arr )) ;
11
+ radixSort ( arr );
12
+ long endTime = System . currentTimeMillis ();
13
+ System . out . println ( "Sorted Array: " + Arrays . toString ( arr ) + " \n Time taken: " +
14
+ (( endTime - startTime )* 0.001 )+ " seconds" ) ;
15
15
}
16
-
17
- // A function to do counting sort of arr[] according to
18
- // the digit represented by exp.
19
- static void countSort (int arr [], int n , int exp )
20
- {
21
- int output [] = new int [n ]; // output array
22
- int i ;
23
- int count [] = new int [10 ];
24
- Arrays .fill (count , 0 );
25
-
26
- // Store count of occurrences in count[]
27
- for (i = 0 ; i < n ; i ++)
28
- count [(arr [i ] / exp ) % 10 ]++;
29
-
30
- // Change count[i] so that count[i] now contains
31
- // actual position of this digit in output[]
32
- for (i = 1 ; i < 10 ; i ++)
33
- count [i ] += count [i - 1 ];
34
-
35
- // Build the output array
36
- for (i = n - 1 ; i >= 0 ; i --) {
37
- output [count [(arr [i ] / exp ) % 10 ] - 1 ] = arr [i ];
38
- count [(arr [i ] / exp ) % 10 ]--;
16
+ private static void radixSort (int [] arr ){
17
+ if (arr .length ==0 )
18
+ return ;
19
+ // Initialising these arrays for sorting the negative and the positive elements separately.
20
+ int [] positive = Arrays .stream (arr ).filter (i -> i >=0 ).toArray ();
21
+ int [] negative = Arrays .stream (arr ).filter (i -> i <0 ).toArray ();
22
+ if (positive .length >0 ){
23
+ int max = positive [0 ];
24
+ // Find the maximum value present
25
+ for (int j : positive ) {
26
+ max = Math .max (max , j );
27
+ }
28
+ // Looping through the elements and applying Counting Sort based on the number of digits
29
+ // present in the maximum element
30
+ for (int exp =1 ; max /exp >0 ; exp *=10 ){
31
+ countingSort (positive , positive .length , exp );
32
+ }
33
+ }
34
+ if (negative .length >0 ){
35
+ // Converting the negative elements in the array to positive for sorting purpose
36
+ for (int i =0 ; i <negative .length ; i ++){
37
+ negative [i ] *= -1 ;
38
+ }
39
+ // Find the maximum value present
40
+ int max = negative [0 ];
41
+ for (int j : negative ) {
42
+ max = Math .max (max , j );
43
+ }
44
+ // Looping through the elements and applying Counting Sort based on the number of digits
45
+ // present in the maximum element
46
+ for (int exp =1 ; max /exp >0 ; exp *=BASE ){
47
+ countingSort (negative , negative .length , exp );
48
+ }
49
+ }
50
+ int index = 0 ;
51
+ // Including the negative elements in the reverse order since (-9 < -1) &
52
+ // the negative elements were sorted based on the absolute values of the elements in the array
53
+ for (int i = negative .length -1 ; i >=0 ; i --) {
54
+ arr [index ++] = -negative [i ];
55
+ }
56
+ // Appending the positive elements after the negative elements in the array
57
+ for (int j : positive ) {
58
+ arr [index ++] = j ;
39
59
}
40
-
41
- // Copy the output array to arr[], so that arr[] now
42
- // contains sorted numbers according to current
43
- // digit
44
- for (i = 0 ; i < n ; i ++)
45
- arr [i ] = output [i ];
46
- }
47
-
48
- // The main function to that sorts arr[] of
49
- // size n using Radix Sort
50
- static void radixsort (int arr [], int n )
51
- {
52
- // Find the maximum number to know number of digits
53
- int m = getMax (arr , n );
54
-
55
- // Do counting sort for every digit. Note that
56
- // instead of passing digit number, exp is passed.
57
- // exp is 10^i where i is current digit number
58
- for (int exp = 1 ; m / exp > 0 ; exp *= 10 )
59
- countSort (arr , n , exp );
60
- }
61
-
62
- // A utility function to print an array
63
- static void print (int arr [], int n )
64
- {
65
- for (int i = 0 ; i < n ; i ++)
66
- System .out .print (arr [i ] + " " );
67
60
}
68
-
69
- // Main driver method
70
- public static void main (String [] args )
71
- {
72
- int arr [] = { 170 , 45 , 75 , 90 , 802 , 24 , 2 , 66 };
73
- int n = arr .length ;
74
-
75
- // Function Call
76
- radixsort (arr , n );
77
- print (arr , n );
61
+ private static void countingSort (int [] arr , int n , int exp ){
62
+ // Initialising the count array with size = BASE
63
+ int [] count = new int [BASE ];
64
+ // Initialising the output array with size = number of elements in positive or negative array
65
+ int [] output = new int [n ];
66
+ // Counting the frequency of the numbers < BASE i.e. from [0..9] in this case
67
+ for (int j : arr ) {
68
+ count [(j / exp ) % BASE ]++;
69
+ }
70
+ // Calculate cumulative count
71
+ for (int i =1 ; i <BASE ; i ++){
72
+ count [i ] += count [i -1 ];
73
+ }
74
+ // Building the output array
75
+ for (int i =n -1 ; i >=0 ; i --){
76
+ output [count [(arr [i ]/exp )%BASE ]-1 ] = arr [i ];
77
+ count [(arr [i ]/exp )%BASE ]--;
78
+ }
79
+ // Copying the array elements present in output array to the original array
80
+ System .arraycopy (output , 0 , arr , 0 , n );
78
81
}
79
82
}
83
+
84
+ /*
85
+ Time Complexity: O(d*(n+b)), d = number of digits
86
+ n = number of elements in the array
87
+ b = the base of the number system being used which is 10 here,
88
+ this can be taken as 20, 30 anything based on the range of elements present,
89
+ but the larger the base the more space will be required.
90
+ Auxiliary Space Complexity: O(n+b), n = number of elements
91
+ b = base of the number system being used which is 10 here
92
+ */
0 commit comments