How to merge to list to another one?

This is a discussion on How to merge to list to another one? within the C++ Programming forums, part of the General Programming Boards category; Read from two file,each file has a list of integers.You must merge them in to a new file(list),and sort them ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    67

    How to merge to list to another one?

    Read from two file,each file has a list of integers.You must merge them in to a new file(list),and
    sort them from smallest to largest.
    I think I need to read these two lists into two arrays from file first,then sort these two lists and write them to the new file!But the problem is after I sort them,How could I merge two arrays to a new one and make it in order?Please give me some hints!Thanks!

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Create a third array and copy items to it one at a time from whichever list has the smallest item. Does that help?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    67
    so you mean each time read in to the two lists one number for each list,then compare this two number and write the smaller one to the third array,but how about the lager one?compare it to the next two numbers?

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    67
    I have a crazy idea:Can I add two streams?I mean like stream3=stream1+stream2;so I merge them into a same stream,then sort.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No. You can of course read all integers into a single array and sort that, which is pretty much what you want anyway. The other option is to read them into two arrays and sort each, then use the merge algorithm to create a single, sorted array from that. This may be the faster option, perhaps.

    To learn about the merge algorithm, seach for "merge sort". Descriptions of this sorting method should describe it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 12:21 PM
  2. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 11:30 AM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 04:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21