Hello everyone i have developed a program for sorting the elements using merge sort.
I want to display each element iteration wise like,
for e.g
Input 4 7 14 1 3 9 17
Iteration 1:- 1 3 4
Iteration 2:- 1 3 4 7
Iteration 3:- 1 3 4 7 9
etc.
Code is as follow:
void mergesort(int *a,int low,int high,int n)
{
int mid,size,it;
size=high-low+1;
if(size<=1)
return;
mid=low + (size/2) - 1;
mergesort(a,low,mid,n);
mergesort(a,mid+1,high,n);
merge(a,low,mid+1,high,n);
textcolor(7);
}
void merge(int *k,int f,int s,int t,int n)
{
int i,j,l,temp[50],b;
static int c=0;
i=f;
j=s;
l=0;
printf("Iteration-%d : ",c+1);
c++;
for(b=0;b<n;b++)
{
printf("%d ",k[b]);
}
printf("\n");
while(i<s) {
if(k[i]<=k[j])
{
l++;
temp[l]=k[i];
i++;
}
else
{
l++;
temp[l]=k[j];
j++;
}
}
if(i>=s)
{
while(j<=t)
{
l++;
temp[l]=k[j];
j++;
}
}
else
{
while(i<s)
{
l++;
temp[l]=k[i];
i++;
}
}
for(i=1;i<=l;i++)
{
k[f-1+i]=temp[i];
}
textcolor(7);
}
Can anyone help me?
Thanks a lot in advance
[edit]Code blocks added, HTML encoded - OriginalGriff[/edit]