-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadixSort.java
More file actions
165 lines (127 loc) · 4.7 KB
/
RadixSort.java
File metadata and controls
165 lines (127 loc) · 4.7 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Random;
public class RadixSort
{
/*
* Radix sort an array of Strings
* Assume all are all ASCII
* Assume all have same length
*/
public static void radixSortA( String [ ] arr, int stringLen )
{
final int BUCKETS = 256;
ArrayList<String> [ ] buckets = new ArrayList<>[ BUCKETS ];
for( int i = 0; i < BUCKETS; i++ )
buckets[ i ] = new ArrayList<>( );
for( int pos = stringLen - 1; pos >= 0; pos-- )
{
for( String s : arr )
buckets[ s.charAt( pos ) ].add( s );
int idx = 0;
for( ArrayList<String> thisBucket : buckets )
{
for( String s : thisBucket )
arr[ idx++ ] = s;
thisBucket.clear( );
}
}
}
/*
* Counting radix sort an array of Strings
* Assume all are all ASCII
* Assume all have same length
*/
public static void countingRadixSort( String [ ] arr, int stringLen )
{
final int BUCKETS = 256;
int N = arr.length;
String [ ] buffer = new String[ N ];
String [ ] in = arr;
String [ ] out = buffer;
for( int pos = stringLen - 1; pos >= 0; pos-- )
{
int[ ] count = new int [ BUCKETS + 1 ];
for( int i = 0; i < N; i++ )
count[ in[ i ].charAt( pos ) + 1 ]++;
for( int b = 1; b <= BUCKETS; b++ )
count[ b ] += count[ b - 1 ];
for( int i = 0; i < N; i++ )
out[ count[ in[ i ].charAt( pos ) ]++ ] = in[ i ];
// swap in and out roles
String [ ] tmp = in;
in = out;
out = tmp;
}
// if odd number of passes, in is buffer, out is arr; so copy back
if( stringLen % 2 == 1 )
for( int i = 0; i < arr.length; i++ )
out[ i ] = in[ i ];
}
/*
* Radix sort an array of Strings
* Assume all are all ASCII
* Assume all have length bounded by maxLen
*/
public static void radixSort( String [ ] arr, int maxLen )
{
final int BUCKETS = 256;
ArrayList<String> [ ] wordsByLength = new ArrayList<>[ maxLen + 1 ];
ArrayList<String> [ ] buckets = new ArrayList<>[ BUCKETS ];
for( int i = 0; i < wordsByLength.length; i++ )
wordsByLength[ i ] = new ArrayList<>( );
for( int i = 0; i < BUCKETS; i++ )
buckets[ i ] = new ArrayList<>( );
for( String s : arr )
wordsByLength[ s.length( ) ].add( s );
int idx = 0;
for( ArrayList<String> wordList : wordsByLength )
for( String s : wordList )
arr[ idx++ ] = s;
int startingIndex = arr.length;
for( int pos = maxLen - 1; pos >= 0; pos-- )
{
startingIndex -= wordsByLength[ pos + 1 ].size( );
for( int i = startingIndex; i < arr.length; i++ )
buckets[ arr[ i ].charAt( pos ) ].add( arr[ i ] );
idx = startingIndex;
for( ArrayList<String> thisBucket : buckets )
{
for( String s : thisBucket )
arr[ idx++ ] = s;
thisBucket.clear( );
}
}
}
public static void main( String [ ] args )
{
List<String> lst = new ArrayList<>( );
Random r = new Random( );
final int LEN = 5;
for( int i = 0; i < 100000; i++ )
{
String str = "";
int len = LEN; // 3 + r.nextInt( 7 ); // between 3 and 9 characters
for( int j = 0; j < len; j++ )
str += (char) ( 'a' + r.nextInt( 26 ) );
lst.add( str );
}
String [ ] arr1 = new String[ lst.size( ) ];
String [ ] arr2 = new String[ lst.size( ) ];
lst.toArray( arr1 );
lst.toArray( arr2 );
long start, end;
start = System.currentTimeMillis( );
Arrays.sort( arr1 );
end = System.currentTimeMillis( );
System.out.println( "Elapsed: " + ( end - start ) );
start = System.currentTimeMillis( );
countingRadixSort( arr2, LEN );
end = System.currentTimeMillis( );
System.out.println( "Elapsed: " + ( end - start ) );
for( int i = 0; i < arr1.length; i++ )
if( !arr1[ i ].equals( arr2[ i ] ) )
System.out.println( "OOPS!!" );
}
}