-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSort
54 lines (49 loc) · 893 Bytes
/
MergeSort
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
#include<stdio.h>
void Merge(int arr[],int start,int mid,int end)
{
int i=start, j=mid+1, k=start;
int sort[end-start+1];
while(i<=mid && j<=end)
{
if(arr[i]<=arr[j])
{
sort[k++]=arr[i++];
}
else if(arr[i]>arr[j])
{
sort[k++]=arr[j++];
}
}
while(i<=mid)
{
sort[k++]=arr[i++];
}
while(j<=end)
{
sort[k++]=arr[j++];
}
for(int q=start; q<k; q++)
{
arr[q]=sort[q];
}
}
void MergeSort(int arr[],int start,int end)
{
if(start==end)
{ return;
}
int mid=(start+end)/2;
MergeSort(arr,start,mid);
MergeSort(arr,mid+1,end);
Merge(arr,start,mid,end);
}
int main()
{
int arr[8]= {35,50,15,25,80,20,90,45};
MergeSort(arr,0,7);
for(int t=0; t<8; t++)
{
printf("%d ",arr[t]);
}
return 0;
}