Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Merging n sorted arrays

Status
Not open for further replies.

lbucko

Programmer
Feb 13, 2002
11
0
0
IE
I need to merge into a single array multiple sorted arrays. I can manage merging two arrays but don't know how to do it for n arrays. Any help would be appreciated. Section of current code shown below:
Code:
while ((indexa<num_site1)&&(indexb<num_site2))
{
	if (site1[indexa]<= site2[indexb])
	{
		res_arr[indexc++]=site1[indexa++];
	}
	else
	{
		(res_arr[indexc++]=site2[indexb++]);
	}
}
while (indexa<num_site1)
{
	res_arr[indexc++]=site1[indexa++];
}
while (indexb<num_site2)
{
	res_arr[indexb++]=site2[indexb++];
}
*num_res_arr = indexc;
 
You can go on merging arrays from 1 to 2, 2 to 4 and so on.
This the way merge sort works. R.Sathish.
babusats@rediffmail.com

 
I realise that however I need create a sorted array of n arrays, is that the only way I can merge them
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top