Skip to content

Commit 4f2b73b

Browse files
authored
Added Exponential Search in PHP (jainaman224#2957)
Update Exponential_Search.php
1 parent dc117b6 commit 4f2b73b

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
// Exponential search in PHP
3+
4+
function Binary_Search($input_arr, $left, $right, $x)
5+
{
6+
if ($right > $left)
7+
{
8+
$mid = $left + ($right - $left) / 2;
9+
if ($input_arr[$mid] == $x)
10+
return $mid;
11+
if ($x < $input_arr[$mid] )
12+
return Binary_Search($input_arr, $left, $mid - 1, $x);
13+
14+
return Binary_Search($input_arr, $mid + 1, $right, $x);
15+
}
16+
return -1;
17+
}
18+
19+
function Exponential_Search($input_arr,$size,$x)
20+
{
21+
if ($input_arr[0] == $x)
22+
return 0;
23+
$i = 1;
24+
while ($i < $size and $input_arr[$i] <= $x)
25+
{
26+
$i = $i * 2;
27+
}
28+
29+
return Binary_Search($input_arr, $i / 2, min($i, $size), $x);
30+
31+
}
32+
33+
//Driver Code
34+
35+
$input_arr = array(1, 2, 3, 4, 5, 6);
36+
$size = count($input_arr);
37+
$x = 4;
38+
$result = exponential_search($input_arr, $size, $x);
39+
if ($result != -1)
40+
echo "The index at which the element " , $x, " is present is " , $result;
41+
42+
else
43+
echo "The element is not present in array";
44+
45+
/*
46+
OUTPUT:
47+
The index at which the element 4 is present is 3
48+
*/
49+
50+
?>

0 commit comments

Comments
 (0)