Merge Sorted Array

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

Merge Sorted Array
 1 public class Solution {
 2     public void merge(int A[], int m, int B[], int n) {
 3         int p3 = m+n-1;
 4         int p1 = m-1;
 5         int p2 = n-1;
 6         while(p1>=0 && p2>=0){
 7             if(A[p1]>=B[p2]){
 8                 A[p3] = A[p1];
 9                 p1--;
10             }
11             else{
12                 A[p3] = B[p2];
13                 p2--;
14             }
15             p3--;
16         }
17         while(p1>=0){
18             A[p3]=A[p1];
19             p3--;p1--;
20         }
21         while(p2>=0){
22             A[p3] = B[p2];
23             p2--;p3--;
24         }
25     }
26 }
View Code

Merge Sorted Array

上一篇:泛型编程与STL学习笔记之容器


下一篇:aix installp软件包管理工具