標籤:blog function utf-8 header cti index har log func
1 <?php 2 //佈建要求頭 3 header("content-type:text/html;charset=utf-8"); 4 5 /* 6 二分差尋找: 7 源:數組 8 條件:必須是有序的資料,要麼從小到大,要麼從大到小 9 10 */11 12 $arr = array(1,2,3,4,5);13 14 /*15 @$arr 源數組16 @$arr_start_index 開始下標17 @$arr_end_index 結束下標18 @$number 要尋找的數19 */20 function search(&$arr, $arr_start_index, $arr_end_index, $number){21 22 //防止下標重合23 if($arr_start_index > $arr_end_index){24 echo ‘不存在改值‘;25 exit;26 }27 28 $arr_center_index = round( ($arr_start_index + $arr_end_index)/2 ); //向下取整29 30 if($arr[$arr_center_index] > $number){31 search($arr, $arr_center_index, $arr_end_index-1, $number);32 }33 34 else if($arr[$arr_center_index] < $number){35 search($arr, $arr_start_index+1, $arr_center_index, $number); 36 }37 38 else if($arr[$arr_center_index] == $number){39 echo ‘存在給值,該值在數組中的下標為:‘ . $arr_center_index;40 exit;41 }42 43 echo ‘不存在改值‘;44 exit;45 }46 47 search($arr, 0, 4,5);48 49 ?>
數組尋找之二分尋找-PHP