-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbinarySearch.php
More file actions
40 lines (31 loc) · 886 Bytes
/
binarySearch.php
File metadata and controls
40 lines (31 loc) · 886 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
//二分查找,返回索引
function binarySearch($arr, $str){
$low = 0;
$high = count($arr) - 1;
while($low<=$high){
$mid = floor(($low + $high)/2);
if($arr[$mid] == $str) return $mid;
if($arr[$mid]>$str) $high = $mid-1;
if($arr[$mid]<$str) $low = $mid +1;
}
return false;//查找失败
}
$arr = array(1, 3, 5, 7, 9, 11);
$inx = binarySearch($arr, 11);
echo $inx."\n";
//递归方式实现
function find($need, $arr) {
$count = count($arr);
if ($count == 1) {
if ($arr[0] == $need) return true;
return false;
}
$tmp = intval($count/2) - 1;
if ($arr[$tmp] == $need) return true;
if ($arr[$tmp] > $need) {
return find($need, array_slice($arr, 0, $tmp+1));
} else {
return find($need, array_slice($arr, $tmp + 1));
}
}