forked from EnigmaVSSUT/Hacktoberfest2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeSort.cpp
109 lines (98 loc) · 2.17 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include<iostream>
#include<algorithm>
using namespace std;
void myMerge(int a[],int p, int q, int r)
{
int i=0,j=0,k=0;
int n1=q-p+1;
int n2=r-q;
int *l=NULL;
l=new int[n1];
int *R=NULL;
R=new int[n2];
int z=1;
/*copying first half of the array into a new one*/
for(i=p;i<=q;i++)
{
l[z]=a[i];
z++;
}
z=1;
/*copying second half of the array into another array*/
for(i=q+1;i<=r;i++)
{
R[z]=a[i];
z++;
}
i=1;j=1;
k=p;
/*comparing the two new array halves and storing the result back into original one*/
while(i!=n1+1 && j!=n2+1)
{
if(l[i]<=R[j])
{
a[k]=l[i];
i++;
k++;
}
else{
a[k]=R[j];
j++;
k++;
}
}
/*used in case one array half is larger than the other*/
/*used if l array finishes but R remains*/
if(i>=n1+1)
{
for(int x=k;x<=n1+n2;x++)
{
a[x]=R[j];
j++;
}
}
else if(j>=n2+1) /* used if R finishes and l remains, so simply copying elements of l into original array*/
{
for(int x=k;x<=n1+n2;x++)
{
a[x]=l[i];
i++;
}
}
}
void mergeSort(int a[],int p, int r)
{
if(p>=r)
return;
int q=0;
/* finding the center of the array*/
if(p<r)
q=(p+r)/2;
/*calling mergesort on two halves of the array and then merging them back into one sorted array*/
mergeSort(a,p,q);
mergeSort(a,q+1,r);
myMerge(a,p,q,r);
}
int main()
{
int *a=NULL;
int n=0;
cout<<"Enter the number of elements MS"<<endl;
cin>>n;
a=new int[n];
/* taking input of the array */
cout<<"Enter the elements of the array"<<endl;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
/*calling mergesort function on the array*/
mergeSort(a,1,n);
/*printing the sorted array*/
cout<<"The sorted array is"<<endl;
for(int j=1;j<=n;j++)
{
cout<<a[j]<<" ";
}
return 0;
}