-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadixSort.java
More file actions
177 lines (147 loc) 路 3.9 KB
/
Copy pathRadixSort.java
File metadata and controls
177 lines (147 loc) 路 3.9 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
166
167
168
169
170
171
172
173
174
175
176
177
package Visualizer.Sorts;
import java.util.ArrayList;
import Visualizer.SortingVisualizer;
import Visualizer.VisualizerFrame;
public class RadixSort implements Runnable{
private Integer[] toBeSorted;
private VisualizerFrame frame;
private boolean lsd;
public RadixSort(Integer[] toBeSorted, VisualizerFrame frame, boolean lsd) {
this.toBeSorted = toBeSorted;
this.frame = frame;
this.lsd = lsd;
}
public void run() {
if (lsd) radixlsd(toBeSorted, 1);
else radixmsd(toBeSorted, findDigit(toBeSorted));
SortingVisualizer.isSorting=false;
}
private void radixlsd(Integer[] x, int digit){
ArrayList<Integer>[] buckets = new ArrayList[10];
for(int i = 0; i<10; i++){
buckets[i] = new ArrayList<Integer>();
}
int theDig = 0;
int maxI = 0;
for(int i = 0; i<x.length; i++){
theDig = (int) (x[i]%Math.pow(10, digit));
for(int t = 0; t<digit-1; t++){
theDig/=10;
}
if (x[i] > maxI) maxI = x[i];
frame.reDrawArray(x, -1, -1, i);
try {
Thread.sleep(SortingVisualizer.sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
buckets[theDig].add(x[i]);
}
ArrayList<Integer> finalList = new ArrayList<>();
for(int i = 0; i<10; i++){
finalList.addAll(buckets[i]);
}
Integer[] y = finalList.toArray(new Integer[0]);
frame.reDrawArray(y);
try {
Thread.sleep(SortingVisualizer.sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (maxI < Math.pow(10, digit)) return;
radixlsd(y, digit+1);
}
private void radixmsd(Integer[] x, int digit){
ArrayList<Integer>[] buckets = new ArrayList[10];
for(int i = 0; i<10; i++){
buckets[i] = new ArrayList<Integer>();
}
int theDig = 0;
for(int i = 0; i<x.length; i++){
theDig = (int) (x[i]%Math.pow(10, digit));
for(int t = 0; t<digit-1; t++){
theDig/=10;
}
frame.reDrawArray(x, -1, -1, i);
try {
Thread.sleep(SortingVisualizer.sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
buckets[theDig].add(x[i]);
}
ArrayList<Integer> finalList = new ArrayList<>();
for(int i = 0; i<10; i++){
finalList.addAll(buckets[i]);
}
Integer[] y = finalList.toArray(new Integer[0]);
frame.reDrawArray(y);
try {
Thread.sleep(SortingVisualizer.sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (digit == 1) return;
int beginning = 0;
for (int i = 0; i < 10; i++) {
y = radixmsd(y, digit-1, beginning, beginning + buckets[i].size());
beginning += buckets[i].size();
}
}
private Integer[] radixmsd(Integer[] x, int digit, int begin, int end){
ArrayList<Integer>[] buckets = new ArrayList[10];
for(int i = 0; i<10; i++){
buckets[i] = new ArrayList<Integer>();
}
int theDig = 0;
for(int i = begin; i<end; i++){
theDig = (int) (x[i]%Math.pow(10, digit));
for(int t = 0; t<digit-1; t++){
theDig/=10;
}
frame.reDrawArray(x, -1, -1, i);
try {
Thread.sleep(SortingVisualizer.sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
buckets[theDig].add(x[i]);
}
ArrayList<Integer> finalList = new ArrayList<>();
for (int i = 0; i < begin; i++) {
finalList.add(x[i]);
}
for(int i = 0; i<10; i++){
finalList.addAll(buckets[i]);
}
for (int i = end; i < x.length; i++) {
finalList.add(x[i]);
}
Integer[] y = finalList.toArray(new Integer[0]);
frame.reDrawArray(y);
try {
Thread.sleep(SortingVisualizer.sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (digit == 1) return y;
int beginning = begin;
for (int i = 0; i < 10; i++) {
y = radixmsd(y, digit-1, beginning, beginning + buckets[i].size());
beginning += buckets[i].size();
}
return y;
}
private int findDigit(Integer[] x) {
int max = Integer.MIN_VALUE;
int digit = 1;
for (int i : x) {
if (i > max) max = i;
}
while (max > 10) {
max = max/10;
digit++;
}
return digit;
}
}