Here you can find the source of unsignedBinarySearch(final short[] array, final int begin, final int end, final short k)
Parameter | Description |
---|---|
array | array where we search |
begin | first index (inclusive) |
end | last index (exclusive) |
k | value we search for |
public static int unsignedBinarySearch(final short[] array, final int begin, final int end, final short k)
//package com.java2s; /*/*from w w w .ja v a 2 s.com*/ * (c) the authors Licensed under the Apache License, Version 2.0. */ public class Main { /** * optimization flag: whether to use hybrid binary search: hybrid formats * combine a binary search with a sequential search */ public static boolean USE_HYBRID_BINSEARCH = true; /** * Look for value k in array in the range [begin,end). If the value is found, return its index. If * not, return -(i+1) where i is the index where the value would be inserted. The array is assumed * to contain sorted values where shorts are interpreted as unsigned integers. * * @param array array where we search * @param begin first index (inclusive) * @param end last index (exclusive) * @param k value we search for * @return count */ public static int unsignedBinarySearch(final short[] array, final int begin, final int end, final short k) { if (USE_HYBRID_BINSEARCH) { return hybridUnsignedBinarySearch(array, begin, end, k); } else { return branchyUnsignedBinarySearch(array, begin, end, k); } } protected static int hybridUnsignedBinarySearch(final short[] array, final int begin, final int end, final short k) { int ikey = toIntUnsigned(k); // next line accelerates the possibly common case where the value would // be inserted at the end if ((end > 0) && (toIntUnsigned(array[end - 1]) < ikey)) { return -end - 1; } int low = begin; int high = end - 1; // 32 in the next line matches the size of a cache line while (low + 32 <= high) { final int middleIndex = (low + high) >>> 1; final int middleValue = toIntUnsigned(array[middleIndex]); if (middleValue < ikey) { low = middleIndex + 1; } else if (middleValue > ikey) { high = middleIndex - 1; } else { return middleIndex; } } // we finish the job with a sequential search int x = low; for (; x <= high; ++x) { final int val = toIntUnsigned(array[x]); if (val >= ikey) { if (val == ikey) { return x; } break; } } return -(x + 1); } protected static int branchyUnsignedBinarySearch(final short[] array, final int begin, final int end, final short k) { int ikey = toIntUnsigned(k); // next line accelerates the possibly common case where the value would // be inserted at the end if ((end > 0) && (toIntUnsigned(array[end - 1]) < ikey)) { return -end - 1; } int low = begin; int high = end - 1; while (low <= high) { final int middleIndex = (low + high) >>> 1; final int middleValue = toIntUnsigned(array[middleIndex]); if (middleValue < ikey) { low = middleIndex + 1; } else if (middleValue > ikey) { high = middleIndex - 1; } else { return middleIndex; } } return -(low + 1); } protected static int toIntUnsigned(short x) { return x & 0xFFFF; } }