-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayOfQueues.java
More file actions
77 lines (73 loc) · 1.71 KB
/
Copy pathArrayOfQueues.java
File metadata and controls
77 lines (73 loc) · 1.71 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
// Tracy Dobbs
// CSC161
// Apr 8, 2020 (Covid-19 era)
import java.util.Queue;
import java.util.LinkedList;
import java.util.ArrayList;
public class ArrayOfQueues
{
public static void main(String[] args)
{
// create an array of 25 3-digit values to be sorted
int[] vals = new int[25];
for(int x=0; x<25; x++)
{
int rVal = (int)(Math.random() * 1000);
while(rVal < 100)
rVal = (int)(Math.random() * 1000);
vals[x] = rVal;
}
System.out.println("Values before sorting:");
for(int x=0; x<24; x++)
{
System.out.print(vals[x] + ", ");
}
System.out.println(vals[24]);
// Create the ten buckets for the radix sort
ArrayList<Queue<Integer>> qArray = new ArrayList<>();
for(int x=0; x<10; x++)
{
Queue<Integer> q = new LinkedList<>();
qArray.add(q);
}
// Begin sort
for(int x=0; x<3; x++) // For each of the three digits
{
for(int y=0; y<25; y++) // in all 25 integers
{
// put numbers in buckets based on digit value
// Determine digit value
int v = vals[y];
int hold = (int)(v / Math.pow((double)(10), (double)(x)));
int d = hold % 10;
// place in bucket determined by d
int w = 0;
for(Queue<Integer> q :qArray)
{
if(w == d)
{
q.offer(v);
}
w++;
}
}
// Pull values from buckets and place in vals array
int y=0;
for(Queue<Integer> q :qArray)
{
int qSize = q.size();
for(int z=0; z<qSize; z++)
{
vals[y] = q.remove();
y++;
}
}
}
System.out.println("\n\nValues after sorting:");
for(int x=0; x<24; x++)
{
System.out.print(vals[x] + ", ");
}
System.out.println(vals[24]);
}
}