C-language implementation of sequential merging of array B on array a _c language

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.