Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C++
// Logic for merge sort
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];
	       //	printf("%d",k[i]);
	}
	textcolor(7);
}   // merge() ends here


Can anyone help me?
Thanks a lot in advance

[edit]Code blocks added, HTML encoded - OriginalGriff[/edit]
Posted
Updated 13-Dec-12 22:40pm
v4
Comments
nv3 14-Dec-12 8:46am    
And what is the problem -- or should we guess?
Stephen Hewison 14-Dec-12 12:23pm    
Why use a merge sort? Try a quick sort. http://www.cquestions.com/2008/01/c-program-for-quick-sort.html
Although the same site covers merge and other sort algorithms.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900