Sep 21, 2017 at 4:56pm UTC
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!
Sep 21, 2017 at 5:09pm UTC
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 Sep 21, 2017 at 5:23pm UTC
Sep 21, 2017 at 5:41pm UTC
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 Sep 21, 2017 at 5:44pm UTC
Sep 21, 2017 at 5:54pm UTC
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 Sep 21, 2017 at 8:25pm UTC
Sep 21, 2017 at 7:08pm UTC
still learning all the algorithm stuff. Slowly. Good call on merge()