//二分法查找的前提,数组需要是有序的
function getIndex(arr,num){
var len = arr.length,
st = 0,
end = len-1
while(st<=end){
var mid = Math.floor((st+end)/2)
if(num==arr[mid]){
return mid
}else if(num>arr[mid]){
st = mid+1
}else{
end = mid-1
}
}
return arr;
}
var ary=[1,4,7,8,12,34,67,88,99,100]
<script>
var a = [1,3,5,7,9,11,13,15];
var min = 0;
var max = a.length - 1;
var findNum = 11;
while(min <= max){
var middle = parseInt((min+max)/2);
if(findNum > a[middle]){
min = middle + 1;
middle = parseInt((min+max)/2);
}
else if(findNum < a[middle]){
max = middle - 1;
middle = parseInt((min+max)/2);
}
else{
console.log("找到了"+middle);
break;
}
}
</script>