-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting(MERGE).cpp
More file actions
69 lines (67 loc) · 916 Bytes
/
Copy pathSorting(MERGE).cpp
File metadata and controls
69 lines (67 loc) · 916 Bytes
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
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<time.h>
using namespace std;
void MERGING(int b[], int L[], int nL ,int R[], int nR)
{
//cout<<"MERGING";
int i,j,k;
i=j=k=0;
while(i<nL&j<nR)
{
if(L[i]<R[j])
{
b[k]=L[i];
k++;
i++;
}
else
{
b[k]=R[j];
k++;
j++;
}
}
while(i<nL)
{
b[k]=L[i];
k++;
i++;
}
while(j<nR)
{
b[k]=R[j];
k++;
j++;
}
}
void mergesort(int a[],int n)
{
//cout<<"mergesort"<<endl;
//cout<<n;
if(n<2)return;
int m=n/2;
int l[m], r[n-m];
for(int i=0;i<m;i++)l[i]=a[i];
for(int i=m;i<n;i++)r[i-m]=a[i];
mergesort(l,m);
mergesort(r,n-m);
MERGING(a,l,m,r,n-m);
}
int main()
{
int t=time(NULL);
srand(t);
int bil[10], n;
cout<<"Before: ";
for(int i=0;i<10;i++)
{
bil[i]=rand()%100+1;
cout<<bil[i]<<" ";
}
mergesort(bil,10);
cout<<endl<<endl;
cout<<"After : ";
for(int i=0;i<10;i++)cout<<bil[i]<<" ";
}