9.11 Sorting and finding (i)--given two sorted arrays A and B, where the end of a has enough buffer space to accommodate B. Merge B into a and sort

Source: Internet
Author: User
/**
* Function: Given two sorted arrays A and B, where the end of a has enough buffer space to accommodate B. Merge B into a and sort.

*/


/** * Problem: If you insert an element into the front of array A, you must move the original element backward to make room. * Idea: Insert an element into the end of array A. *  Note: After processing the elements of B, you do not need to copy the remaining elements of a, because those elements are originally in a. * @param A * @param b * @param lasta * @param lastb */public static void merge (int[] a,int[] b,int lasta,int lastb) {int in Dexa=lasta-1;int indexb=lastb-1;int Indexmerged=lasta+lastb-1;while (indexa>=0&&indexb>=0) {if (a[ INDEXA]>B[INDEXB]) {a[indexmerged]=a[indexa];indexmerged--;indexa--;} else{a[indexmerged]=a[indexb];indexmerged--;indexb--;}} while (indexb>=0) {a[indexmerged]=a[indexb];indexmerged--;indexb--;}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

9.11 Sorting and finding (i)--given two sorted arrays A and B, where the end of a has enough buffer space to accommodate B. Merge B into a and sort

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.