Created
May 26, 2013 15:04
-
-
Save bittib/5653042 to your computer and use it in GitHub Desktop.
Merge Two Sorted Arrays
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void merge(int A[], int m, int B[], int n) { | |
int k = n + m - 1; | |
while (m > 0 && n > 0){ | |
if (A[m-1] > B[n-1]) | |
A[k--] = A[--m]; | |
else | |
A[k--] = B[--n]; | |
} | |
while (n > 0) | |
A[k--] = B[--n]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment