/**
* 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