Holyjesus

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.1 kB
14
Indexable
Never
#include<stdio.h>
int arr[50];
int b[50];
mergesort(int low,int high)
{
	int mid;
	if(low<high)
	{
		mid=(low+high)/2;
		mergesort(low,mid);
	    mergesort(mid+1,high);
	    merge(low,mid,high);
	}
}
merge(int low,int mid,int high)
{
	int i,j,h;
	h=low;
	i=low;
	j=mid+1;
	while((h<=mid)&&(j<=high))
	{
		if(arr[h]<arr[j])
		{
			b[i]=arr[h];
			h++;
			i++;
		}
		else
		{
			b[i]=arr[j];
			j++;
			i++;
		}
	}
		while(h<=mid)
		{
			b[i++]=arr[h++];
		}
		while(j<=high)
		{
			b[i++]=arr[j++];
		}
		printf("\nThe auxilary array is:\n");
		for(i=low;i<=high;i++)
		{
			printf("%d\t",b[i]);
			//printf("\");
		}
		for(i=low;i<=high;i++)
		arr[i]=b[i];
}
void main()
{
	int i,n,low,high,mid;
	printf("Enter limit of the array:\t");
	scanf("%d",&n);
	printf("Enter elements in the array to be sorted:\n");
	for(i=0;i<n;i++)
	{
		scanf("%d",&arr[i]);
	}
	low=0;
	high=n-1;
	mergesort(low,high);
//	merge(low,mid,high);
	printf("The sorted array is:\n");
	for(i=0;i<n;i++)
	{
		printf("%d\t",arr[i]);
	}
}