The principle is the merge sort merge sort (MERGE-SORT) is to establish an efficient sorting algorithm on a merge operation, the algorithm is very typical application uses a divide and conquer (Divide and Conquer) a. The combined sequence has been ordered, fully ordered sequence; i.e., each first ordered sequence, and then ordered to make inter-segment sequences. If two merged into a sorted list ordered list, called way merge.
Merge operation works as follows:
The first step: the application space, so that the size of the sum of two sorted sequences, the sequence space used to store the merged
Step 2: Set two pointers, respectively, two original position a starting position of the sequence have been ordered
third step: compare two pointer points to an element, the selected element into a relatively small space to merge, move the pointer to the next location and
repeat step 3 until the end of the sequence of a pointer beyond
the All the remaining elements of another sequence directly copied to the end of the sequence were combined
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int a, int b)
{
return a > b ;
}
int main()
{
int b[3] = {1,5,7};
int a[4] = {6,3,2,4};
int c[7];
int *p, *q;
p = &b[0];
q = &a[0];
sort(q, q + 4,cmp);
sort(p,p+3,cmp);
for (int i = 0; i < 7; i++)
{
if (*q > *p)
{
c[i] = *q; q++;
}
else if(*p>*q)
{
c[i] = *p; p++;
}
else if (*p == *q)
{
c[i] = *p; p++,q++;
}
}
for (int i = 0; i<7; i++)
{
cout << c[i] << ' ';
}
system("pause");
}