亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

javascript 折半查找字符在數(shù)組中的位置(有序列表)

 更新時(shí)間:2010年12月09日 18:21:05   作者:  
折半查找字符在數(shù)組中的位置(有序列表),需要的朋友可以參考下。
復(fù)制代碼 代碼如下:

/**
* 折半查找字符在數(shù)組中的位置(有序列表)
* @param array 被檢索的數(shù)組
* @param x 要查找的字符
* @type int
* @returns 字符在數(shù)組中的位置,沒找到返回-1
*/

function binarySearch(array,x){
var lowPoint=1;
var higPoint=array.length;
var returnValue=-1;
var midPoint;
var found=false;
while ((lowPoint<=higPoint)&&(!found)){
midPoint=Math.ceil((lowPoint+higPoint)/2);
//console.log(lowPoint+"===="+midPoint+"===="+higPoint);
if(x>array[midPoint-1]){
lowPoint=midPoint+1;
}
else if(x<array[midPoint-1]){
higPoint= midPoint-1;
}
else if(x=array[midPoint-1]){
found=true;
}

}
if(found){
returnValue=midPoint;
}
return returnValue;
}
/*var array2=[1,2,3,4,5,6,7,8,9,100,109];*/
var array2=['a','b','c','d','e','f','g'];
console.log(binarySearch(array2,'c'));

相關(guān)文章

最新評論