-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartitionSort.java
82 lines (65 loc) · 1.65 KB
/
PartitionSort.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
import java.awt.Color;
import java.lang.Thread;
import javax.swing.JLabel;
public class PartitionSort extends Thread{
private BuildGUI gui;
private Rank rank;
private JLabel label;
private int[] array;
private final int sortNum = 4;
public PartitionSort(int[] array, BuildGUI gui, Rank rank) {
this.gui = gui;
this.rank = rank;
this.array = array;
this.label = gui.getSortLabel(sortNum);
}
public void run() {
long startTime, endTime, duration;
startTime = System.currentTimeMillis();
partitionSort(array);
endTime = System.currentTimeMillis();
duration = endTime - startTime;
label.setText(Long.toString(duration));
setRank();
}
public static void partitionSort(int[] array) {
partitionSort(array, 0, array.length - 1);
}
public static void partitionSort(int[] array, int low, int high) {
if(low < high) {
int pivot, left = low, right = high;
pivot = array[left];
while(left < right) {
while(pivot < array[right] && left < right) {
right--;
}
array[left] = array[right];
while(pivot >= array[left] && left < right) {
left++;
}
array[right] = array[left];
}
array[left] = pivot;
partitionSort(array, low, left - 1);
partitionSort(array, right + 1, high);
}
}
public void setRank() {
int place = rank.getPlacement();
Color color ;
rank.incrementPlacement();
if(place == 1) {
color = new Color(255, 215, 0);
}
else if(place == 2) {
color = new Color(192, 192, 192);
}
else if(place == 3) {
color = new Color(205, 127, 50);
}
else {
color = new Color(255, 255, 255);
}
gui.setSortColor(sortNum, color);
}
}