/**
*
*/
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));
}
}

 

 posted on 2010-08-29 14:48  ﹎敏ō  阅读(165)  评论(0编辑  收藏  举报