forked from keshavagarwal17/All-Cp-Algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSort.java
96 lines (86 loc) · 2.67 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import java.util.*;
public class MergeSort{
public static void mergesort(int p,int q,int [] arr)
{
if(p<q)
{
int k=(p+q)/2;
mergesort(p,k,arr);
mergesort(k+1,q,arr);
merge(arr,p,k,q);
}
}
public static void merge(int [] arr,int p,int k,int q)
{
int n1=k-p+2;
int n2=q-k+1;
int []l1=new int [n1];
int []l2=new int [n2];
l1[n1-1]=Integer.MAX_VALUE;
l2[n2-1]=Integer.MAX_VALUE;
for(int i=0;i<n1-1;i++)
{
l1[i]=arr[i+p];
}
for(int i=0;i<n2-1;i++)
{
l2[i]=arr[i+k+1];
}
int i=0,j=0;
for(int b=p;b<=q;b++)
{
if(l1[i]<=l2[j])
{
arr[b]=l1[i];
i++;
}
else
{
arr[b]=l2[j];
j++;
}
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Random rand = new Random();
int n;
System.out.print("Enter the size of the array : ");
n = scan.nextInt();
int[] a = new int[n];
int[] b = new int[n];
int[] c = new int[n];
int r = 3*n;
for(int i=0;i<n;i++)
{
a[i] = i;
b[i] = n-i;
c[i] = rand.nextInt(r);
}
int[] a1 = new int [n];
int[] b1 = new int [n];
int[] c1 = new int [n];
for(int j = 0 ; j < 15 ; j++)
{
for(int i = 0 ; i < n ; i++)
{
a1[i] = a[i];
b1[i] = b[i];
c1[i] = c[i];
}
final long startTimeA = System.nanoTime();
mergesort(0,n-1,a1);
final long durationA = (System.nanoTime() - startTimeA);
final long startTimeB = System.nanoTime();
mergesort(0,n-1,b1);
final long durationB = (System.nanoTime() - startTimeB);
final long startTimeC = System.nanoTime();
mergesort(0,n-1,c1);
final long durationC = (System.nanoTime() - startTimeC);
System.out.println("The time required for merge sort for a[] is : " + durationA + " nanoseconds.");
System.out.println("The time required for merge sort for b[] is : " + durationB + " nanoseconds.");
System.out.println("The time required for merge sort for c[] is : " + durationC + " nanoseconds.");
System.out.println(" ");
}
}
}