output 2 arrays in order

How can I put 2 arrays together but in order considering that both arrays are sorted?

like:
1 3 5 8 9
2 4 6 7 10

output : 1 2 3 4 5 6 7 8 9 10;

the initial size of the arrays are 5 always;

I tried many ways but it does not solve the problem. Thanks!
you merge sort the 2 into a third, or you can dump them into a third and then sort the third with your current sort approach.

I recommend the 2nd approach for 2 reasons:
1) the better sorts do reduced work for partially sorted, pre-sorted, and similar data. Including the built in c++ sort function. This data scenario is closer to best case (almost no work to do) than worst case (the N lg N run time lots of swaps needed).

2) you would have to write your own merge sort here, rather than use built-in which means more work, more debugging, and worse performance than #1.
Last edited on
Don't have to write your own merge sort, std lib has one in <algorithm>. O(n) complexity instead of O(n log n)!

But to be clear: It's undefined behavior if the v1 and v2 are not already sorted.
http://en.cppreference.com/w/cpp/algorithm/merge

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iterator>
#include <iostream>
#include <algorithm>

int main()
{
    std::vector<int> v1 {1, 3, 5, 8, 9};
    std::vector<int> v2 {2, 4, 6, 7, 10};
    
     // merge
    std::vector<int> dst;
    std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(dst));
 
    // output : 1 2 3 4 5 6 7 8 9 10;
    std::cout << "dst: ";
    std::copy(dst.begin(), dst.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
}
Last edited on
Hello unkinkedash,

jonnin has a good approach but for something simple and because I o not know what your program looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
	constexpr size_t MAXSIZE{ 5 };

	int odd[MAXSIZE]{ 1,3,5,7,9 };
	int even[MAXSIZE]{ 2,4,6,8,10 };

	std::cout << " ";

	for (size_t lc = 0; lc < MAXSIZE; lc++)
	{
		std::cout << odd[lc] << " " << even[lc] << ((lc < 4) ? " " : ";");
		// Or 
		//std::cout << odd[lc] << " " << even[lc] << " ";
	}


Hope that helps,

Andy
It's not too hard to do from scratch:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>

int main() 
{    
    int a[] = {1, 3, 5, 8, 9};
    int b[] = {2, 4, 6, 7, 10};    
    
    const int asize = sizeof(a) / sizeof(a[0]);
    const int bsize = sizeof(b) / sizeof(b[0]);
        
    int ax = 0;
    int bx = 0;
    
    while (ax < asize || bx < bsize)
    {
        if (ax < asize && bx < bsize)
        {
            if (a[ax] < b[bx])
            {
                std::cout << a[ax] << "  ";
                ax++;
            }
            else                            
            {
                std::cout << b[bx] << "  ";
                bx++;
            }                
        }
        else if (ax < asize)
        {
            std::cout << a[ax] << "  ";            
            ax++;
        }
        else
        {
            std::cout << b[bx] << "  ";
            bx++;
        }                 
    }

    std::cout << '\n';
}


Or a little more concise, but same as before:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using std::cout;

int main() 
{    
    const int size = 5;
    
    int a[size] = {1, 3, 5, 8, 9};
    int b[size] = {2, 4, 6, 7, 10};    
            
    int ax = 0;
    int bx = 0;
    
    while (ax < size || bx < size)
    {
        if (ax < size && bx < size)
            cout << ( (a[ax] < b[bx]) ? a[ax++] : b[bx++] ) << "  ";
        else if (ax < size)
            cout << a[ax++] << "  ";            
        else
            cout << b[bx++] << "  ";              
    }

    std::cout << '\n';
}


Of course, once you start using the conditional operator, it's easy to get carried away...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using std::cout;

int main() 
{    
    const int size = 5;
    
    int a[size] = {1, 3, 5, 8, 9};
    int b[size] = {2, 4, 6, 7, 10};    
            
    for (int ax = 0, bx = 0; ax<size||bx<size; )
        cout << ((ax<size&&bx<size)?((a[ax]<b[bx])?a[ax++]:b[bx++]):((ax<size)?a[ax++]:b[bx++])) << ' ';
    cout << '\n';
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main() 
{    
    const int size = 5;
    
    int a[size] = {1, 3, 5, 8, 9};
    int b[size] = {2, 4, 6, 7, 10};
    int c[2*size];
            
    int ax = 0;
    int bx = 0;
    int cx = 0;
    
    while ( ax < size )
    {
        if ( bx == size || a[ax] < b[bx] ) c[cx++] = a[ax++];
        else                               c[cx++] = b[bx++];
    }
    while ( bx < size ) c[cx++] = b[bx++];           

    for ( int i : c ) cout << i << ' ';
}
still learning all the algorithm stuff. Slowly. Good call on merge()
Topic archived. No new replies allowed.