Merge Sorted Array

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+n1;
04      int i=m1;
05      int j=n1;
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
This entry was posted in Array and linked list. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s