-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.java
83 lines (71 loc) · 1.91 KB
/
QuickSort.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.io.*;
import java.awt.*;
import javax.swing.JLabel;
public class QuickSort extends Sort
{
private String name = "Quick Sort";
public int num;
long initialTime;
public QuickSort(JLabel label)
{
super(label);
}
public void Sort(Rect[] arr, SorterCanvas canvas)
{
initialTime = System.currentTimeMillis();
Sort(arr, canvas, 0, arr.length-1);
}
public void Sort(Rect[] a, SorterCanvas canvas, int start, int end)
{
if(start<end)
{
timeLabel.setText("Time Elapsed (ms): "+(System.currentTimeMillis() - initialTime));
int q=partition(a, canvas, start,end);
Sort(a, canvas, start,q);
Sort(a, canvas, q+1,end);
}
}
public int getNum()
{
return num;
}
public String getName()
{
return name;
}
public String toString()
{
return getName();
}
public int partition(Rect[] a, Canvas canvas, int start, int end) {
int x = a[start].getNum();
int i = start-1 ;
int j = end+1 ;
while (true) {
timeLabel.setText("Time Elapsed (ms): "+(System.currentTimeMillis() - initialTime));
i++;
while ( i< end && a[i].getNum() < x)
i++;
j--;
while (j>start && a[j].getNum() > x)
j--;
if (i < j)
{
timeLabel.setText("Time Elapsed (ms): "+(System.currentTimeMillis() - initialTime));
swap(a, i, j);
//num++;
canvas.repaint();
try{ Thread.sleep(50); }
catch(Exception e) {}
}
else
return j;
}
}
public void swap(Rect[] arr, int i, int j)
{
Rect temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}