Java Binary Search binarySearch(final Object[] a, final int fromIndex, final int toIndex, final Object key)

Here you can find the source of binarySearch(final Object[] a, final int fromIndex, final int toIndex, final Object key)

Description

binary Search

License

Apache License

Declaration

@SuppressWarnings("unchecked")
    public static int binarySearch(final Object[] a, final int fromIndex, final int toIndex, final Object key) 

Method Source Code

//package com.java2s;
/*/*from w  ww. java 2  s. co m*/
 * @(#)$Id: ArrayUtils.java 3619 2008-03-26 07:23:03Z yui $
 *
 * Copyright 2006-2008 Makoto YUI
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Contributors:
 *     Makoto YUI - ported from jakarta commons lang
 */

public class Main {
    @SuppressWarnings("unchecked")
    public static int binarySearch(final Object[] a, final int fromIndex, final int toIndex, final Object key) {
        int low = fromIndex;
        int high = toIndex - 1;

        while (low <= high) {
            int mid = (low + high) >>> 1;
            Comparable midVal = (Comparable) a[mid];
            int cmp = midVal.compareTo(key);

            if (cmp < 0) {
                low = mid + 1;
            } else if (cmp > 0) {
                high = mid - 1;
            } else {
                return mid; // key found
            }
        }
        return -(low + 1); // key not found.
    }

    public static int compareTo(final byte lhs[], final byte rhs[]) {
        if (lhs == rhs) {
            return 0;
        }
        final int len = Math.min(lhs.length, rhs.length);
        for (int i = 0; i < len; i++) {
            if (lhs[i] != rhs[i]) {
                return (lhs[i] & 0xFF) - (rhs[i] & 0xFF);
            }
        }
        return lhs.length - rhs.length;
    }

    public static int compareTo(final byte lhs[], final byte rhs[], final int offset) {
        final int len = Math.min(lhs.length, rhs.length);
        for (int i = offset; i < len; i++) {
            if (lhs[i] != rhs[i]) {
                return (lhs[i] & 0xFF) - (rhs[i] & 0xFF);
            }
        }
        return lhs.length - rhs.length;
    }

    public static int compareTo(final byte lhs[], final byte rhs[], final int offset, final int length) {
        int tolen = offset + length;
        final int limit = Math.min(Math.min(lhs.length, rhs.length), tolen);
        for (int i = offset; i < limit; i++) {
            if (lhs[i] != rhs[i]) {
                return (lhs[i] & 0xFF) - (rhs[i] & 0xFF);
            }
        }
        return 0;
    }

    public static int compareTo(final byte[] b1, final int off1, final int len1, final byte[] b2, final int off2,
            final int len2) {
        for (int i = 0; i < len1 && i < len2; i++) {
            final int d = (b1[off1 + i] & 0xFF) - (b2[off2 + i] & 0xFF);
            if (d != 0) {
                return d;
            }
        }
        return len1 - len2;
    }

    public static int compareTo(final int lhs[], final int rhs[]) {
        if (lhs == rhs) {
            return 0;
        }
        final int len = Math.min(lhs.length, rhs.length);
        for (int i = 0; i < len; i++) {
            if (lhs[i] != rhs[i]) {
                return lhs[i] - rhs[i];
            }
        }
        return lhs.length - rhs.length;
    }
}

Related

  1. binarySearch(double values[], double search)
  2. binarySearch(double[] a, double key)
  3. binarySearch(double[] arr, double value, int p, int q)
  4. binarySearch(final double[] array, final double val)
  5. binarySearch(final Enum[] full, final int[] partial, final char key, final int index)
  6. binarySearch(int arr[], int low, int high, int key)
  7. binarySearch(int codePoint)
  8. binarySearch(int[] array, int key)
  9. binarySearch(int[] array, int size, int value)