-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRadixMerge.java
91 lines (81 loc) · 1.94 KB
/
RadixMerge.java
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
import java.io.*;
import java.awt.*;
import javax.swing.JLabel;
public class RadixMerge extends Sort
{
private String name = "Radix Sort (Merge)";
public int num;
public RadixMerge(JLabel label)
{
super(label);
}
public void Sort(Rect[] arr, SorterCanvas canvas)
{
Sort(arr, canvas, arr.length-1);
}
public void Sort(Rect[] arr, SorterCanvas canvas, int stop)
{
int highest = 0;
Bucket[] buckets = new Bucket[10];
boolean sorted = false;
for(int i = 0; i<10; i++)
{
buckets[i] = new Bucket();
}
for(int a = 0; a< stop; a++)
{
int ar = arr[a].getNum();
if(ar>highest)
{
highest = ar;
}
}
int expo = 0;
boolean done = false;
while(!done)
{
highest = highest/10;
expo++;
if(highest<=0)
done = true;
}
expo--;
sorted = true;
for(int i = 0; i<stop; i++)
{
Rect items = arr[i];
int item = items.getNum();
int buck = (item/((int)(Math.pow(10, expo))));
buck = buck%10;
buckets[buck].addEnd(items);
}
int index = 0;
for(int j = 0; j<buckets.length; j++)
{
Bucket b = buckets[j];
while(b.isFilled())
{
arr[index] = b.removeLast();
index++;
num++;
canvas.repaint();
try{ Thread.sleep(10); }
catch(Exception e) {}
}
}
MergeSort m = new MergeSort(timeLabel);
m.Sort(arr, canvas);
}
public int getNum()
{
return num;
}
public String getName()
{
return name;
}
public String toString()
{
return getName();
}
}