473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

About Sorting

mhk
Hi ,
is there any way to merge three sorted arrays into a sorted file,
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.

Thank you veryyyyy much.

Jeff

Nov 13 '05 #1
5 3031

"mhk" <cc*****@dddsssl.com> schrieb im Newsbeitrag
news:3F**************@dddsssl.com...
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[i],
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
Nov 13 '05 #2
mhk
Thanks Robert,

i just have three sorted arrays ,and i have to merge all three arrays
into an empty file such that the file is also sorted.

thanks

jeff

************************************************** *

Robert Stankowic wrote:
"mhk" <cc*****@dddsssl.com> schrieb im Newsbeitrag
news:3F**************@dddsssl.com...
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[i],
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


Nov 13 '05 #3
mhk wrote:

is there any way to merge three sorted arrays into a sorted file,
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.


It bears a startling resemblance to the problem of selecting the
minimum (or maximum) from among three possibilities. It is not a
C language problem, thus off-topic here.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #4
mhk wrote:
Thanks Robert,

i just have three sorted arrays ,and i have to merge all three arrays
into an empty file such that the file is also sorted.


Then do precisely what Robert suggested. Set each index to 0, compare
indices, find the smallest, stick it in the file and bump that index,
continue until you run out of data.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #5
On Sun, 23 Nov 2003 09:51:05 -0600, mhk <cc*****@dddsssl.com> wrote:
Hi ,
is there any way to merge three sorted arrays into a sorted file,
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.


Sorted arrays a1, a2, and a3. Indexes i1, i2, and i3. File f.
Pseudo code:

i1 = i2 = i3 =0
while (1)
if (a1[i1] < a2[i2])
if (a1[i1] < a3[i3])
fwrite(&a1[i1], sizeof *a1, 1, f)
i1++
else
fwrite(&a3[i3], sizeof *a3, 1, f)
i3++
else
if (a2[i2] < a3[i3])
fwrite(&a2[i2], sizeof *a2, 1, f)
i2++
else
fwrite(&a3[i3], sizeof *a3, 1, f)
i3++

No fourth array at all. You need additional logic to handle:
different length arrays
the fact that two of the arrays will be exhausted before the third
when to terminate the loop
<<Remove the del for email>>
Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
13
by: agentxx04 | last post by:
Hi. Our assignment was to creat a program that can find the average, median & mode of a #of integers. Here's my program: #include<stdio.h> int main() { int item; int a, b, t, mode; int...
4
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
1
by: PiPOW | last post by:
Hi. I have a question. I am developing an application and I have to choose the most suitable control to show records from some database queries, with freedom in customizing control layout and...
4
by: Ambica Jain | last post by:
Hi, I want custom sorting on some of the columns in the datagrid. And i am able to do the same by overriding MouseDown event. However, i need to rebind my datatable to reflect the changes in...
1
by: Budiman | last post by:
I want to learn about sorting. Can you explain about bubble sort, quick sort, selection sort etc.(if posssible give me some simple example program of each sorting) And what kind of sorting is the...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
160
by: raphfrk | last post by:
Is this valid? int a; void *b; b = (void *)a; // b points to a b += 5*sizeof(*a); // b points to a a = 100;
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.