Two-part lookup and array merging

Source: Internet
Author: User

/**
* Array Merging
*/
public static int[] Arraymerge (int[] A, int[] b) {
Int[] C = new Int[a.length + b.length];
int k=0,j=0,i=0;
while (k< a.length && J <b.length) {
if (A[k] <= b[j]) {
C[i] = a[k];
k++;
} else {
C[i] = B[j];
j + +;
}
i++;
}
if (k = = a.length) {
for (int n = j; n < b.length; n++) {
C[k+n] = B[n];
}
} else {
for (int n = k; n < a.length; n++) {
C[k+n] = A[n];
}
}
return C;
}

/** * 二分法查找 */public static int binarySearch(int[] a, int x){    return binarySearch(a, x, 0, a.length);}private static int binarySearch(int[] a, int x, int left, int right) {    int high = right-1;    while (left<=high){        count++;        int mid = (left + high)/2;        int tem = a[mid];        if (tem > x)            high = mid-1;        else if (tem < x)            left = (mid+1);        else            return mid;    }    return -1;}

Two-part lookup and array merging

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.