二分查找
1 <? 2 //二分查找(递归消除) 3 function bin_sch($array, $n, $k){ 4 $low = 0; 5 $high = $n-1; 6 while($low <= $high){ 7 $mid = intval(($high-$low)/2); 8 if ($array[$mid] == $k) 9 return $mid; 10 elseif ($k < $array[$mid]){ 11 $high = $mid - 1; 12 }else{ 13 $low = $mid + 1; 14 } 15 } 16 return -1; 17 } 18 19 //顺序查找(改进版) 20 function seq_sch($array, $n, $k){ 21 $array[$n] = $k; 22 for($i=0; ; $i++){ 23 if($array[$i]==$k){ 24 break; 25 } 26 } 27 if ($i<$n){ 28 return $i; 29 }else{ 30 return -1; 31 } 32 } 33 ?>