Tweet
int swap(int *a,int x,int y)
{
int temp=*(a+x);
*(a+x)=*(a+y);
*(a+y)=temp;
}
void merge(int a[],int n)
{
int c[n],k=0;
int i,j;
for(i=0;i<n/2;i++)
for(j=0;j<n/2-1;j++)
if(a[j]>a[j+1])
swap(a,j,j+1);
printf("\nBubble output 1 : ");
for(i=0;i<n/2;i++)
printf("%4d",a[i]);
for(i=n/2;i<n;i++)
for(j=n/2;j<n-1;j++)
if(a[j]>a[j+1])
swap(a,j,j+1);
printf("\nBubble output 2 : ");
for(i=n/2;i<n;i++)
printf("%4d",a[i]);
printf("\n");
int p=0,q=n/2;
while((p<=n/2)&&(q<=n-1))
{
if(a[p]<=a[q])
c[k++]=a[p++];
else
c[k++]=a[q++];
}
while(p<=n/2)
c[k++]=a[p++];
while(q<=n-1)
c[k++]=a[q++];
printf("Sorted array : ");
for(i=0;i<n;i++)
printf("%4d",c[i]);
printf("\n");
}
main()
{
int i,n;
//int a[]={45,23,12,56,76,54,34,33,21,22};
printf("\n\nEnter array capacity : \n");
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
*(a+i)=rand()%100;
printf("Initial array : ");
for(i=0;i<n;i++)
printf("%4d",a[i]);
printf("\n");
merge(a,n);
}
#include<stdio.h>