473,770 Members | 5,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3061

"mhk" <cc*****@dddsss l.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_typ e> 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(whatev er 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_fi le>) /*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*****@dddsss l.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_typ e> 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(whatev er 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_fi le>) /*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********@yah oo.com) (cb********@wor ldnet.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.pow ernet.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*****@dddsss l.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
2533
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() sorted_feature_vector.sort() #feature_vector.keys()=sorted_feature_vector
13
2201
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 median_index;
4
2815
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
25466
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 sort columns based on the column header the user has clicked in both Ascending and Descending formats.
1
1189
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 behavior, leaving data read-only. Good examples might be: control showing message list in Microsoft Outlook (or Outlook Express); download-in-progress (or upload-in-progress) control in Emule client; every common XP-style window showing files and...
4
3101
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 grid. And with rebinding, sorting image (little triangle on the column header) goes away. I need to show sorting image as well custom sorting. Please help. Thanks
1
1000
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 best. Thx for all your attention
5
3186
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 search algorithms that can be used are Linear Search and Binary Search. The sorting algorithms are bubble, selection and Insertion sort. First, the user is asked whether he/she wants to perform a search option, a sort operation, or exit the program. If...
160
5704
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
4956
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 there are, it crashes. There are currently 6 columns, and I only want 4. How do I remove the last two (discount and date)? Here is a link: http://www.jaredmoore.com/tablesorter/docs/salestable.html Here is some jquery js that I think...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10225
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8880
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3969
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.