Merge Sorted Array

Source: Internet
Author: User

Merge Sorted Array

Problem:

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

Note:
You could assume that A have enough space (size that's greater or equal to m + n) to the hold additional Eleme NTS from B. The number of elements initialized in A and B is m andn respectively.

Ideas:

Merge sort finishing Work + just need to move then we'll come back.

My Code:

 Public classSolution { Public voidMergeintA[],intMintB[],intN) {if(A = =NULL|| B = =NULL|| M + n = = 0)return; intLast = m + n-1; inti = m-1, j = n-1;  while(I >= 0 && J >= 0)        {            if(A[i] >B[j]) {A[last--] = a[i--]; }            Else{a[last--] = b[j--]; }        }         while(I >= 0) A[last--] = a[i--];  while(J >= 0) A[last--] = b[j--]; return; }}
View Code

Merge Sorted Array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.