-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge_sort.c
68 lines (63 loc) · 1.1 KB
/
merge_sort.c
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
#include <stdio.h>
int arr[25],b[25];
void Merge(int arr[],int low,int mid,int high)
{
int i,j,k=0,p;
i=low;
j=mid+1;
while(i <= mid && j <= high)
{
if(arr[i] < arr[j])
{
b[k]=arr[i];
k++;i++;
}
else if(arr[j] < arr[i])
{
b[k]=arr[j];
k++;j++;
}
}
while(i <= mid)
{
b[k]=arr[i];
i++;k++;
}
while(j <= high)
{
b[k]=arr[j];
j++;k++;
}
for (p=0;p<=high-low;p++)
{
arr[low+p]=b[p];
}
}
void MergeSort(int arr[],int low,int high)
{
int mid;
if(low < high)
{
mid=(low + high)/2;
MergeSort(arr,low,mid);
MergeSort(arr,mid+1,high);
Merge(arr,low,mid,high);
}
}
void main()
{
int n;
printf("How many elements in array ? : ");
scanf("%d",&n);
printf("Enter the elements : ");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
MergeSort(arr,0,n-1);
printf("After Sorting : ");
for(int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
}