<html>
<head>
</head>
<script type="text/javascript">
<!--
//二分查找
var arr=[1,2,3,4,5,67,86,52,100];
function binarySearch(arr,findVal,leftIndex,rightIndex){
if(leftIndex>rightIndex){
document.writeln("didn't find!!");
return;
}
var midIndex=Math.floor((leftIndex+rightIndex)/2);
var midVal=arr[midIndex];
if(midVal>findVal){
binarySearch(arr,findVal,leftIndex,midIndex-1);
}else if(midVal<findVal){
binarySearch(arr,findVal,midIndex+1,rightIndex)
}else{
document.writeln("find num:"+midIndex);
return;
}
}
binarySearch(arr,67,0,arr.length-1);
-->
</script>
<body>
</body>
</html>
二分查找
最新推荐文章于 2024-10-21 22:00:00 发布