/**
*
*/
package org.adaikiss.algorithm.search;
/**
* @author hlw
*
*/
public class Search {
public static int binarySearch(int[] array, int key){
int low = 0;
int high = array.length-1;
while(low<=high){
int middle = (low+high)>>>1;
if(array[middle]<key) low = middle+1;
else if(array[middle]>key) high = middle-1;
else return middle;
}
return -1;
}
/**
* @param args
*/
public static void main(String[] args) {
int[] array = {1,3,4,5,7,12,34};
System.out.println(binarySearch(array, 7));
System.out.println(binarySearch(array, 8));
}
}