Merge Sorted Array

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

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.

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 }
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: Logo

You are commenting using your 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