2 Dynamic arrays (sorting)

I have two dynamic arrays for example
1
2
int*first=new int [c];
int*second=new int [d];

In this arrays i have random numbers and i would like to sort in specific order in both. Can anybody tell me the fastest way to sort.

so i wish :
if i have numbers :
FIRST array 1,5,35,2,7
SECOND array -1,4,8,3
i would like to sort it :
-1,1,2,3,4,5,7,8,35
and i would like to do it without creating new dynamic arrays[c+d]
IS THIS POSSIBLE?
*Sory for my English
And where exactly do you want the sorted values to go?
than i would like to print it
Can be done with boost from http://www.boost.org (what can't be?)

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
#include <iostream>
#include <algorithm>
#include <boost/range.hpp>
#include <boost/range/join.hpp>
#include <boost/range/algorithm.hpp>
void show(int* p1, int n1, int* p2, int n2)
{
    std::cout << "Arrays contain: ";
    for(int n=0; n<n1; ++n)
        std::cout << p1[n] << ' ';
    std::cout << ": ";
    for(int n=0; n<n2; ++n)
        std::cout << p2[n] << ' ';
    std::cout << '\n';
}
const int a1[] = {1, 5, 35, 2, 7};
const int a2[] = {-1, 4, 8, 3};
int main()
{
    int c = 5;
    int* first = new int[c];
    std::copy(a1, a1+c, first);

    int d = 4;
    int* second = new int[d];
    std::copy(a2, a2+d, second);

    show(first, c, second, d);

    sort(join(boost::make_iterator_range(first, first+c),
              boost::make_iterator_range(second, second+d)));

    show(first, c, second, d);

    delete[] first;
    delete[] second;
}
Arrays contain: 1 5 35 2 7 : -1 4 8 3
Arrays contain: -1 1 2 3 4 : 5 7 8 35
Last edited on
You can do it without boost as well:

1
2
3
sort(first,first+c);
sort(second,second+d);
merge(first,first+c,second,second+d,ostream_iterator<int>(cout," "));


A less efficient way is to use selection sort.
Last edited on
+1 Athar. I didn't see the "I would like to print it" requirement in time.
tnx
Topic archived. No new replies allowed.