Hi ,
is there any way to merge three sorted arrays into a sorted file,
This is not clear to me (maybe my english):
Do you already have a file with some sorted content and want to merge the
arrays into it or do you just want to merge the arrays and write the merged
result?
However, the solution below should work similar in both cases..
without using 4th array. i guess 3 way merge sort is the only option but
i dont know its algorithem.
can anyone tell me algorithm of 3 way merge sort or anyother way to
solve this problem.
There should be no problem.
Just fopen() your output file, use three indices into your sorted arrays,
compare the contents at the indices and write the next matching to the file,
incrementing the index of that array.
(untested pseudocode only)
size_t i = 0;
size_t j = 0;
size_t k = 0;
<yor_item_type> some_var; /*if you have a sorted input file*/
/*If you want to merge into an existing sorted file, open input file and
test for success*/
/*open output file and test for success*/
do
{
/*If you have a sorted input file, read one item from it into
some_var(whatever type it may be in your case)*/
/*find the max or min (depending on your sort order) of array1
,
array2[j], array3[k], and some_var if you have an input file*/
/*write it to the file*/
/*depending on which was max or min*/
/*i++; or
j++; or
k++; or
read the next item from the input file if you have one
*/
}while(i < sizeof array1 || j < sizeof array2 || k < sizeof array3 ||
!feof(<input_file>) /*if an input file exists*/);
HTH
Robert