Java Binary Search binarySearch0(float[] a, int fromIndex, int toIndex, float key)

Here you can find the source of binarySearch0(float[] a, int fromIndex, int toIndex, float key)

Description

binary Search

License

Apache License

Declaration

private static int binarySearch0(float[] a, int fromIndex, int toIndex, float key) 

Method Source Code

//package com.java2s;
/**/*  ww w  . ja v a 2s .co  m*/
 * 
 *    Copyright 2017 Florian Erhard
 *
 *   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.
 * 
 */

public class Main {
    private static int binarySearch0(float[] a, int fromIndex, int toIndex, float key) {
        int low = fromIndex;
        int high = toIndex - 1;

        while (low <= high) {
            int mid = (low + high) >>> 1;
            float midVal = a[mid];

            int cmp;
            if (midVal < key) {
                cmp = -1; // Neither val is NaN, thisVal is smaller
            } else if (midVal > key) {
                cmp = 1; // Neither val is NaN, thisVal is larger
            } else {
                int midBits = Float.floatToIntBits(midVal);
                int keyBits = Float.floatToIntBits(key);
                cmp = (midBits == keyBits ? 0 : // Values are equal
                        (midBits < keyBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
                                1)); // (0.0, -0.0) or (NaN, !NaN)
            }

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

    private static int binarySearch0(double[] a, int fromIndex, int toIndex, double key) {
        int low = fromIndex;
        int high = toIndex - 1;

        while (low <= high) {
            int mid = (low + high) >>> 1;
            double midVal = a[mid];

            int cmp;
            if (midVal < key) {
                cmp = -1; // Neither val is NaN, thisVal is smaller
            } else if (midVal > key) {
                cmp = 1; // Neither val is NaN, thisVal is larger
            } else {
                long midBits = Double.doubleToLongBits(midVal);
                long keyBits = Double.doubleToLongBits(key);
                cmp = (midBits == keyBits ? 0 : // Values are equal
                        (midBits < keyBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
                                1)); // (0.0, -0.0) or (NaN, !NaN)
            }

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

Related

  1. binarySearch(long[] data, long key, int low, int high)
  2. binarySearch(Object[] a, Object key)
  3. binarySearch(String[] arr, String value)
  4. binarySearch(String[] array, String sought)
  5. binarySearch(T[] a, T x)
  6. binarySearchCeil(double[] a, double key)
  7. binarySearchIndex(double[] values, double toFind)
  8. binarySearchLower(int[] theSearchBase, int theLowerBound, int theUpperBound, int theKey)
  9. binarySearchReversed(int[] a, int fromIndex, int toIndex, int key)