-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOddEvenSort.java
76 lines (66 loc) · 1.89 KB
/
OddEvenSort.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
import java.io.*;
import java.awt.*;
import javax.swing.JLabel;
public class OddEvenSort extends Sort
{
private String name = "Odd-Even Sort";
public int num;
public OddEvenSort(JLabel label)
{
super(label);
}
public void Sort(Rect[] arr, SorterCanvas canvas)
{
boolean sorted = false;
long initialTime = System.currentTimeMillis();
while(!sorted)
{
timeLabel.setText("Time Elapsed (ms): "+(System.currentTimeMillis() - initialTime));
sorted=true;
for(int i = 1; i < arr.length-1; i += 2)
{
if(arr[i].getNum() > arr[i+1].getNum())
{
timeLabel.setText("Time Elapsed (ms): "+(System.currentTimeMillis() - initialTime));
swap(arr, i, i+1);
num++;
canvas.repaint();
try{ Thread.sleep(3); }
catch(Exception e) {}
sorted = false;
}
}
for(int i = 0; i < arr.length-1; i += 2)
{
if(arr[i].getNum() > arr[i+1].getNum())
{
timeLabel.setText("Time Elapsed (ms): "+(System.currentTimeMillis() - initialTime));
swap(arr, i, i+1);
num++;
canvas.repaint();
try{ Thread.sleep(10); }
catch(Exception e) {}
sorted = false;
}
}
}
}
public int getNum()
{
return num;
}
public void swap(Rect[] arr, int i, int j)
{
Rect temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public String getName()
{
return name;
}
public String toString()
{
return getName();
}
}