fun(arr, low, high, key){
if(low > high){
return -1;
}
var mid = (high + low)/2;
if( arr[mid] == key){
return mid;
}else if( arr[mid] > key ){
high = mid - 1;
return fun(arr, low, high, key);
}else if( arr[mid] < key ){
high = mid + 1;
return fun(arr, low, high, key);
}
}
var arr = [1,2,3,4,5,6,7,8,9,10,11,23,44,86];
var result = fun(arr, 0, 44, 10);
alert(result);
1 回答
已采纳
Caballarii
TA贡献1123条经验 获得超629个赞
function fun(arr, low, high, key){ if(low > high){ return -1; } var mid = (high + low)/2; if( arr[mid] == key){ return mid; }else if( arr[mid] > key ){ high = mid - 1; return fun(arr, low, high, key); }else if( arr[mid] < key ){ low = mid + 1; return fun(arr, low, high, key); } } var arr = [1,2,3,4,5,6,7,8,9,10,11,23,44,86]; var result = fun(arr, 0, arr.length, 10); alert(result);
添加回答
举报
0/150
提交
取消