#include<stdio.h>
#define MAX 10
void MergeSort(int Arr[],int low, int high);
void Merge(int Arr[],int low, int mid,int high);
main()
{
int A[MAX]={9,8,7,6,5,4,3,2,1,10};
int i;
MergeSort(A,0,MAX-1);
for(i=0;i<MAX;i++)
printf("%d ",A[i]);
}
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 Merge(int Arr[], int low, int mid, int high)
{
int l,m,i,k;
int temp[MAX];
l=i=low;
m=mid+1;
while((l<=mid) && (m<=high))
{
if(Arr[l]<=Arr[m])
{
temp[i]=Arr[l];
l++;
}
else
{
temp[i]=Arr[m];
m++;
}
i++;
}
if(l>mid)
{
for(k=m;k<=high;k++)
{
temp[i]=Arr[k];
i++;
}
}
else
{
for(k=l;k<=mid;k++)
{
temp[i]=Arr[k];
i++;
}
}
for(k=low;k<=high;k++)
Arr[k]=temp[k];
}
Time Complexity: O(n logn)
No comments:
Post a Comment