-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_Search
More file actions
38 lines (29 loc) · 876 Bytes
/
binary_Search
File metadata and controls
38 lines (29 loc) · 876 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
/**
LinearSearch | Binary Search | BubbleSort | SelectionSort | Factoral written in PHP
$list = [1,2,3,3,44,5,6,2,3,2,33,8,8,6,"Kanyin",9,24,21,3,',44,5,102,33,43,"James", "Johnson", "Joe", "John", "Jacob", "Josh","Jasmine"];
binarySearch( 4, $list, 1, 6);`
*/
`function binarySearch( $target, array $list, $low=null, $high=null )
{
$numList = count( $list );
$low = 0;
$high = $numList;
if( $low > $high )
{
echo "Not found!";
return false;
}
$middle = ( $low + $high ) / 2;
if( $target == $list[$middle] )
{
echo "Found $target at $middle";
return true;
}
else if($target > $list[ $middle ])
{
return binarySearch( $target, $list, $middle + 1, $high );
}
else{
return binarySearch( $target, $list, $low, $middle - 1 );
}
}`