Here you can find the source of binarySearch(long[] a, long key, int endIndex)
Parameter | Description |
---|---|
a | the array to search in |
key | the key to search for |
endIndex | the limit for the search |
public static int binarySearch(long[] a, long key, int endIndex)
//package com.java2s; //License from project: Apache License public class Main { /**// w w w . j a v a 2 s . c o m * @param a * the array to search in * @param key * the key to search for * @param endIndex * the limit for the search * @return the index of the key to search for */ public static int binarySearch(long[] a, long key, int endIndex) { int low = 0; int high = endIndex - 1; while (low <= high) { int mid = (low + high) >>> 1; long midVal = a[mid]; if (midVal < key) low = mid + 1; else if (midVal > key) high = mid - 1; else return mid; } return -(low + 1); } }