forked from w0rthy/ArrayVisualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSortOOP.java
More file actions
71 lines (66 loc) · 1.87 KB
/
MergeSortOOP.java
File metadata and controls
71 lines (66 loc) · 1.87 KB
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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package array.visualizer;
import static array.visualizer.ArrayVisualizer.*;
import static array.visualizer.Swaps.*;
/**
*
* @author S630690
*/
public class MergeSortOOP {
public static void mergeSortOP()throws Exception {
int start = 0;
int end = array.length;
int mid = (end+start)/2;
mergeOP(start,mid,end);
}
public static void mergeOP(int start, int mid, int end)throws Exception{
if(start==mid)
return;
mergeOP(start, (mid+start)/2, mid);
mergeOP(mid, (mid+end)/2, end);
int[] tmp = new int[end-start];
int low = start;
int high = mid;
for(int nxt = 0; nxt < tmp.length; nxt++){
if(low >= mid && high >= end)
break;
if(low < mid && high >= end){
tmp[nxt]=array[low];
low++;
comps+=2;
}
else if(low >= mid && high < end){
tmp[nxt]=array[high];
high++;
comps+=2;
}
else if(array[low]<array[high]){
tmp[nxt]=array[low];
low++;
comps+=3;
}
else{
tmp[nxt]=array[high];
high++;
comps+=3;
}
aa++;
marked.set(1,low);
marked.set(2, high);
if(end-start>=array.length/10)
sleep(1);
}
//System.arraycopy(tmp, 0, array, start, tmp.length);
marked.set(2, -5);
for(int i = 0; i < tmp.length; i++){
array[start+i]=tmp[i];
aa++;
marked.set(1, start+i);
if(end-start>=array.length/100)
sleep(1);
}
}
}