This article describes the C language implementation in the array a ordered combination of array B method, share for everyone to reference. The specific analysis is as follows:
Topic: Arrays A and B are all ordered, array A has enough memory to hold array B, and array B is merged into array a in order
Analysis: If merging from front to rear, the complexity will be O (N2), so the complexity is obviously not the optimal solution, using two pointers to the tail of two arrays, traversing backwards, such complexity is O (n2)
This allows you to write the following code:
#include <iostream> #include <algorithm> #include <iterator> using
namespace Std;
int arraya[10] = {1, 3, 5, 7, 9};
int arrayb[] = {2, 4, 6, 8, 10};
const int Sizeb = sizeof arrayb/sizeof *arrayb;
const int Sizea = sizeof arraya/sizeof *arraya-sizeb; int* mergearray (int *arraya, int sizea, int *arrayb, int sizeb) {if (Arraya = null | | arrayb = NULL | | Sizea < 0 | |
Sizeb < 0) return NULL;
int posa = sizeA-1;
int PosB = sizeB-1; while (posa >= 0 && posB >= 0) {if (Arraya[posa] < ARRAYB[POSB]) {arraya[posa + PosB + 1] = Arrayb
[PosB];
posb--;
else {arraya[posa + PosB + 1] = Arraya[posa];
posa--;
Copy (Arraya, Arraya +, ostream_iterator<int> (cout, ""));
System ("pause");
return Arraya;
} void Main () {int *result = Mergearray (Arraya, Sizea, Arrayb, Sizeb);
Copy (result, result + ten, ostream_iterator<int> (cout, ""));
cout << Endl; }
The code appears to complete the required functionality, but more than this, you must do the above code UT
1. Robustness
Arraya or Arrayb is empty with a length less than 0
2. Boundary Use Cases
Arraya is empty, length is 1;arrayb not empty, length is greater than 1
First element use case
const int size = 6;
int Arraya[size] = {2};
int arrayb[] = {0, 1, 1, 1, 1};
Instead
const int size = 6;
int Arraya[size] = {0, 1, 1, 1, 1};
int arrayb[] = {2};
3. Normal Use cases:
const int size = 10;
int Arraya[size] = {1, 3, 5, 7, 9};
int arrayb[] = {2, 4, 6, 8, 10};
const int size = 10;
int Arraya[size] = {2, 4, 6, 8, 10};
int arrayb[] = {1, 3, 5, 7, 9};
const int size = 10;
int Arraya[size] = {1, 2, 3, 4, 5};
int arrayb[] = {6, 7, 8, 9, 10};
const int size = 10;
int Arraya[size] = {6, 7, 8, 9, 10};
int arrayb[] = {1, 2, 3, 4, 5};
After the test, it is not difficult to find that in a boundary condition use case, the code has not run the results correctly, in the test case driven, it is not difficult to write the correct code as follows:
int* mergearray (int *arraya, int sizea, int *arrayb, int sizeb)
{
if (Arraya = null | | arrayb = NULL | | Sizea &L T 0 | | Sizeb < 0) return
NULL;
int posa = sizeA-1;
int posB = sizeB-1;
while (posa >= 0 && posB >= 0)
{
if (Arraya[posa] < ARRAYB[POSB])
{
Arraya[posa + PosB + 1] = ARRAYB[POSB];
posb--;
}
else
{
Arraya[posa + PosB + 1] = Arraya[posa];
posa--;
}
Copy (Arraya, Arraya + size, ostream_iterator<int> (cout, ""));
System ("pause");
}
There are two scenarios:
//1 posa < 0 && PosB >= 0
//2. Posa >= 0 && PosB < 0
//Only the 1th case needs to be processed
if (Posa < 0 && posB >= 0)
{While
(PosB >= 0)
{
Arraya[posa + PosB + 1] = ARRAYB[POSB];
posb--
}
}
return Arraya;
}
It is believed that this article has certain reference value to everybody C program algorithm design learning.