标签:fun || value 查找 lse 快速排序 end tar dex
$arr = [1,2,3,3,4,5,6]; function binSearch($arr,$num,$start,$end){ if($start > $end){ return -1; } $mid = floor(($start+$end)/2); $midValue = $arr[$mid]; if($num < $midValue){ $mid = $mid-1; return binSearch($arr,$num,$start,$mid); }elseif($num > $midValue){ $mid = $mid+1; return binSearch($arr,$num,$mid,$end); }else{ if($mid == 0 || $num != $arr[$mid-1]){ return $mid; }else{ return binSearch($arr,$num,$start,$mid-1); } } } $start=0; $end = count($arr)-1; $index = binSearch($arr,3,$start,$end); echo $index;
标签:fun || value 查找 lse 快速排序 end tar dex
原文地址:https://www.cnblogs.com/zh718594493/p/12089231.html