forked from mrizky-kur/Redux-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerge.java
69 lines (68 loc) · 1.79 KB
/
Merge.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
import java.util.*;
import javax.swing.event.DocumentEvent.ElementChange;
public class Merge {
public static void main(String arg[]){
Scanner sc = new Scanner(System.in);
System.out.println("enter size of array:");
int n=sc.nextInt();
int []a=new int[n];
for(int i=0;i<n;i++){
a[i]=n-i;
}
long s =System.nanoTime();
Merge ob = new Merge();
ob.merge_sort(a,0,a.length - 1);
int count= n-1;
long e=System.nanoTime();
System.out.println("total time is: "+(e-s));
System.out.println("number of merge call is:"+count);
System.out.println("sorted array is:");
for(int i=0;i<n;i++){
System.out.println(""+a[i]);
}
}
void merge_sort(int a[],int p,int r){
if(p < r){
int q = p + (r-p)/2;
merge_sort(a, p, q);
merge_sort(a, q+1, r);
merge(a,p,q,r);
}
}
void merge(int a[],int p,int q,int r){
int n1=q-p+1;
int n2=r-q;
int []l1 = new int[n1];
int []r1 = new int[n2];
for(int i=0;i<n1;i++){
l1[i]=a[p+i];
}
for(int j=0;j<n2;j++){
r1[j]=a[q+1+j];
}
int i=0;
int j=0;
int k=p;
while(i<n1 && j<n2){
if(l1[i]<=r1[j]){
a[k]=l1[i];
i++;
}
else{
a[k]=r1[j];
j++;
}
k++;
}
while (i < n1) {
a[k] = l1[i];
i++;
k++;
}
while (j < n2) {
a[k] = r1[j];
j++;
k++;
}
}
}