-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSort.java
73 lines (61 loc) · 1.58 KB
/
MergeSort.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
import java.io.*;
import java.awt.*;
import javax.swing.JLabel;
public class MergeSort extends Sort
{
private String name = "Merge Sort";
public int num;
long initialTime;
public MergeSort(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[] arr, SorterCanvas canvas, int low, int high)
{
if (low >= high)
return;
int mid = (int)((low+high)/2);
Sort(arr, canvas, low, mid);
Sort(arr, canvas, mid+1, high);
while(low <=mid && mid+1<= high)
{
if (arr[mid+1].getNum()>arr[low].getNum())
{
low++;
}
else
{
Rect temp = arr[mid+1];
for (int test = mid; test>=low; test--)
{
arr[test+1] = arr[test];
num++;
timeLabel.setText("Time Elapsed (ms): "+(System.currentTimeMillis() - initialTime));
canvas.repaint();
try{ Thread.sleep(3); }
catch(Exception e) {}
}
arr[low] = temp;
mid++;
low++;
}
}
}
public int getNum()
{
return num;
}
public String getName()
{
return name;
}
public String toString()
{
return getName();
}
}