You are here:

C++/C++ Programming

Advertisement


Question
two one-dimensional arrays A and B which are stored in assending order.to merge them into a single sorted array C that contains every item from arrays A and B, in ascending order,

Answer
here is a simple non-recursive pseudocode implementation to merge two arrays a, b into array result:

function merge( array a, array b, array result )

       make result an empty array

       initialize int i, j to 0
  
               while (i < length(a)) and (j < length(b))
               if a[i] < b[j]
                       append a[i] to result
                       increment i
               else
                       append b[j] to result
                       increment j

       while i < length(a)
               append a[i] to result
               increment i

       while j < length(b)
               append b[j] to result
               increment j  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++09. i would not answer questions about gui and web programming.

Experience

over 15 years

Education/Credentials
post graduate engineer

©2012 About.com, a part of The New York Times Company. All rights reserved.