-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergesort.cpp
More file actions
61 lines (56 loc) · 872 Bytes
/
Mergesort.cpp
File metadata and controls
61 lines (56 loc) · 872 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
/* merge sort */
#include<stdio.h>
void mergesort(int [],int,int);
void merge(int [],int,int,int,int);
main()
{
int a[100],i,n,lb,ub;
printf("enter the length of the array ");
scanf("%d",&n);
printf("enter the terms of the array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
lb=0;ub=n-1;
mergesort(a,lb,ub);
printf("After Sorting\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
return(0);
}
void mergesort(int a[],int lb,int ub)
{
int mid;
mid=(lb+ub)/2;
if(lb<ub)
{
mergesort(a,lb,mid);
mergesort(a,mid+1,ub);
merge(a,lb,mid,mid+1,ub);
}
}
void merge(int a[],int low1,int up1,int low2,int up2)
{
int p,q,n;
int d[100];
p=low1;q=low2;n=0;
while((p<=up1) && (q<=up2))
{
d[n++]=a[p]<a[q]?a[p++]:a[q++];
}
while(p<=up1)
{
d[n++]=a[p++];
}
while(q<=up2)
{
d[n++]=a[q++];
}
for(q=low1,n=0;q<=up2;q++,n++)
{
a[q]=d[n];
}
}