Merging two arrays.

This is a discussion on Merging two arrays. within the C Programming forums, part of the General Programming Boards category; Hello All, I have two arrays. Some elements are duplicated, it really depends on data length at runtime. I want ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    Merging two arrays.

    Hello All,

    I have two arrays. Some elements are duplicated, it really depends on data length at runtime. I want to merge the arrays.

    Wondering, can anybody suggest me best method to merge the arrays.

    First algo comes to my mind is,
    1. Sort one of the array.
    2. Binary-search each element of second array in sorted array.
    3. Place non-duplicated element in third array.
    4. Copy all the elements of sorted array into third array after all the elements in second array have sorted.

    Note:
    1. Array1 and array 2 are of same size. If number of elements are less than actual array length then, remaining elements will be zeroed.
    2. I will allocated third element double the size of first two arrays.

    Disadvatage with this is, we need to allocate third array.

    Second algorithm can be,
    Create binary tree of longer array. Then keep on adding elements of second array. Drop adding to tree if element duplicated otherwise add to tree.

    If you have better and efficient algo. Please let me know..
    Last edited by Roaring_Tiger; 08-20-2004 at 10:44 PM.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    I am looking at mergesort algorith. I will let you people know, if I need help.

    Had become bit impatient. -

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    www.aihorizon.com has some good articles about how to make sort functions efficient.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. merging arrays
    By acohrockz21 in forum C Programming
    Replies: 28
    Last Post: 03-09-2008, 01:28 AM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM
  5. merging arrays using pointers help!!!
    By edshaft in forum C++ Programming
    Replies: 3
    Last Post: 12-19-2001, 06:19 AM

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