コードをコピーします コードは次のとおりです:
/**
* 見つけてください
*
**/
// 逐次検索
functionnormal_search($arrData,$val) {
$len = count($arrData );
if($len == 0) return -1;
for($i = 0;$i <$len; $i++ ) {
echo "検索番号",$i + 1 ," value = ",$arrData[$i]," is = ",$val,"?
"
// 見つかりました
if($arrData[$i] == $val) return $i;
}
return -1;
//テスト順序検索
$arrData = array(4,51,6,73,2,5,9,33,50,3,4,6,1 ,4, 67);
echonormal_search($arrData,6),"
";
echonormal_search($arrData,66),"
"; (順序付き列検索の場合)
function binary_search($arrData,$val) {
$len = count($arrData)
if($len == 0) return -1;
$start = 0; = $len - 1;
while($start <= $end) {
$middle = intval(($start + $end)/2);
echo "start = ",$start," end = " ,$end," middle = ",$middle,"
";
if($arrData[$middle] == $val) {
return $middle; elseif ($arrData[$ middle] ] > $val) {
$end = $middle - 1 ;
} elseif ($arrData[$middle] < $val) {
$start = $middle + 1 ;
return -1 ;
}
//バイナリ検索をテストします
$arrData = array(1,2,3,4,5,7,8,9,11,23,56,100,104,578,1000);
echo binary_search($arrData, 578); "
"
echo binary_search($arrData,66),"
";
http://www.bkjia.com/PHPjc/325529.html
www.bkjia.com
true
http://www.bkjia.com/PHPjc/325529.html
技術記事次のコードをコピーします。 ?php /*** 探す * **/ // 逐次検索関数normal_search($arrData,$val) { $len = count($arrData) if($len == 0) return -1; ; for($i = 0;$i $len; $i++...