My issue
i am unable to understand the logic that how to print the sample output
My code
#include <stdio.h>
void conquer(int a[],int first,int mid,int last)
{
int i,j,t[last+1],k=0;
i=first;
j=mid+1;
while(i<=mid && j<=last)
{
if(a[i]<=a[j])
t[k++]=a[i++];
else
t[k++]=a[j++];
}
while(i<=mid)
t[k++]=a[i++];
while(j<=last)
t[k++]=a[j++];
k=0;
for(i=first;i<=last;i++)
{
a[i]=t[k++];
}
}
void divide(int a[],int first,int last)
{
if(first<last)
{
int mid=first+(last-first)/2;
divide(a,first,mid);
divide(a,mid+1,last);
conquer(a,first,mid,last);
}
}
int main() {
// your code goes here
int n;
scanf("%d",&n);
int a[n],i;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
divide(a,0,n-1);
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
Problem Link: merge sort Practice Coding Problem