-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathMergeSort.cpp
86 lines (85 loc) · 1.31 KB
/
MergeSort.cpp
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
#include<iostream>
#include<stdlib.h>
#include<process.h>
#include<time.h>
using namespace std;
void merge(int a[], int p, int q, int r)
{
int nl = q-p+1, nr = r-q, i, j, k;
int *L = new int[nl], *R = new int[nr];
for(i=0; i<=nl; i++) {
L[i] = a[p+i-1];
cout<<i;
}
for(j=0; j<=nr; j++) {
R[j] = a[q+j];
}
i=j=k=0;
while(i<nl && j<nr)
{
if(L[i]<R[j])
{
a[k]=L[i];
i++;
}
else
{
a[k]=R[j];
j++;
}
k++;
}
while(i<nl)
{
a[k]=L[i];
i++;
k++;
}
while(j<nr)
{
a[k]=R[j];
j++;
k++;
}
delete[] L;
delete[] R;
}
void mergeSort(int a[], int p, int r)
{
int q;
if(p < r) {
q = (p+r)/2;
mergeSort(a, p, q);
mergeSort(a, q+1, r);
merge(a, p, q, r);
}
}
int main() {
int a[1000], size=1000, ch, i;
cout<<"Merge Sort\nEnter your Choice:\n1. Best Case\n2. Worst Case\n3. Average Case\n4. Exit\t";
cin>>ch;
switch(ch) {
case 1: for(i=0; i<1000; i++) {
a[i] = i+1;
}
break;
case 2: for(i=1000; i>0; i--) {
a[1000-i] = i;
}
break;
case 3: srand(time(NULL));
for(i=0; i<1000; i++) {
a[i] = rand() % 1000 + 1;
}
break;
case 4: exit(0);
break;
default: cout<<"Wrong Choice!";
break;
}
clock_t start, end;
start = clock();
mergeSort(a, 0, size-1);
end = clock();
cout<<"Time Taken: "<<(double)(end-start)/CLK_TCK;
}