-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_Sort.cpp
More file actions
71 lines (67 loc) · 848 Bytes
/
Merge_Sort.cpp
File metadata and controls
71 lines (67 loc) · 848 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
70
71
#include<bits/stdc++.h>
using namespace std;
void merge(int a[],int low,int mid,int high)
{
int i,j,k;
int b[1000];
k=low;
i=low;
j=mid+1;
while(i<=mid && j<=high)
{
if(a[i]>a[j])
{
b[k]=a[j];
j++;
}
else
{
b[k]=a[i];
i++;
}
k++;
}
if(i>mid)
{
for(i=j;i<=high;i++)
{
b[k]=a[i];
k++;
}
}
else if(j>high)
{
for(j=i;j<=mid;j++)
{
b[k]=a[j];
k++;
}
}
for(i=low;i<=high;i++)
a[i]=b[i];
}
void merge_sort(int a[],int l,int h)
{
int m;
if(l<h)
{
m=(l+h)/2;
merge_sort(a,l,m);
merge_sort(a,m+1,h);
merge(a,l,m,h);
}
}
int main()
{
int n,i,j,temp;
printf("Enter the array size:");
scanf("%d",&n);
int a[n];
printf("Enter the array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
merge_sort(a,0,n-1);
printf("Sorted array is:\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}