-
Notifications
You must be signed in to change notification settings - Fork 0
/
Merge sort.cpp
86 lines (75 loc) · 1.23 KB
/
Merge sort.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<bits/stdc++.h>
using namespace std;
void merge(int arr[],int l,int mid,int r);
void mergesort(int arr[],int l,int r);
int main()
{
// freopen("input.text","r",stdin);
// freopen("output.text","w",stdout);
int ar[1000],n;
cout<<"How namy elements on this array : ";
cin>>n;
for (int i=0;i<n;i++)
{
cin>>ar[i];
}
mergesort(ar,0,n-1);
for (int i=0;i<n;i++)
cout<<" "<<ar[i];
cout<<endl;
return 0;
}
void mergesort(int arr[],int l,int r)
{
if (l < r)
{
int mid=l+(r-l)/2;
mergesort(arr,l,mid);
mergesort(arr,mid+1,r);
merge(arr,l,mid,r);
}
}
void merge(int arr[],int l,int mid,int r)
{
int i,j,k;
int n1=mid -l+1;
int n2=r-mid;
int le[n1],ri[n2];
for(i=0;i<n1;i++)
{
le[i]=arr[l+i];
}
for(j=0;j<n2;j++)
{
ri[j]=arr[mid+1+j];
}
i=0;
j=0;
k=l;
while(i < n1 && j < n2)
{
if (le[i] <= ri[j])
{
arr[k]=le[i];
i++;
}
else
{
arr[k]=ri[j];
j++;
}
k++;
}
while(i<n1)
{
arr[k]=le[i];
i++;
k++;
}
while(j<n2)
{
arr[k]=ri[j];
j++;
k++;
}
}