java二分法查找
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
?
/** * 二分法查找 * @param intArray 查找的数据组(数组必须有顺序) * @param searchValue 查找的值 * @return int 数组的索引值(index),如果没有找到,则返回-1 */ public final static int search(int[] intArray, int searchValue){ if(intArray != null && intArray.length > 0){ int minIndex = 0; int maxIndex = intArray.length - 1; int middleIndex = 0; while(minIndex <= maxIndex){ middleIndex = (minIndex + maxIndex) / 2; if(intArray[middleIndex] == searchValue){ return middleIndex; }else if(intArray[middleIndex] > searchValue){ maxIndex = middleIndex - 1; }else{ minIndex = middleIndex + 1; } } } return -1; }
??
?
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
原文:http://fanshuyao.iteye.com/blog/2221309