**Given two sorted integer arrays A and B, merge B into A as one sorted array.**

**Note:**

**You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.**

C++:

01 void merge(int A[], int m, int B[], int n)

02 {

03 int t=m+n–1;

04 int i=m–1;

05 int j=n–1;

06 while((i>=0)&&(j>=0)){

07 if(A[i]>=B[j])

08 A[t—]=A[i—];

09 else

10 A[t—]=B[j—];

11 }

12 while(j>=0)

13 A[t—]=B[j—];

14 }

02 {

03 int t=m+n–1;

04 int i=m–1;

05 int j=n–1;

06 while((i>=0)&&(j>=0)){

07 if(A[i]>=B[j])

08 A[t—]=A[i—];

09 else

10 A[t—]=B[j—];

11 }

12 while(j>=0)

13 A[t—]=B[j—];

14 }

Advertisements