8
8
**** if it were in the list.
9
9
* In the second stage, a binary search is performed on this range.
10
10
*/
11
- /**
12
- * @param Array $arr
13
- * @param int $value
14
- * @param int $floor
15
- * @param int $ceiling
16
- * @return int
17
- **/
11
+
12
+ /**
13
+ * @param Array $arr
14
+ * @param int $value
15
+ * @param int $floor
16
+ * @param int $ceiling
17
+ * @return int
18
+ **/
18
19
function binarySearch ($ arr , $ value , $ floor , $ ceiling )
19
20
{
20
-
21
- // Get $middle index
21
+ // Get $middle index
22
22
$ mid = floor (($ floor + $ ceiling ) / 2 );
23
23
// Return position if $value is at the $mid position
24
24
if ($ arr [$ mid ] === $ value ) {
25
25
return (int ) $ mid ;
26
26
}
27
- //Return -1 is range is wrong
27
+ //Return -1 is range is wrong
28
28
if ($ floor > $ ceiling ) {
29
29
return -1 ;
30
30
}
31
- // search the left part of the $array
32
- // If the $middle element is greater than the $value
31
+
32
+ // search the left part of the $array
33
+ // If the $middle element is greater than the $value
33
34
if ($ arr [$ mid ] > $ value ) {
34
35
return binarySearch ($ arr , $ value , $ floor , $ mid - 1 );
35
- }
36
- // search the right part of the $array
37
- // If the $middle element is lower than the $value
38
- else {
36
+ } else { // search the right part of the $array If the $middle element is lower than the $value
39
37
return binarySearch ($ arr , $ value , $ mid + 1 , $ ceiling );
40
38
}
41
39
}
@@ -47,13 +45,12 @@ function binarySearch($arr, $value, $floor, $ceiling)
47
45
*/
48
46
function exponentialSearch ($ arr , $ value )
49
47
{
50
-
51
- // If $value is the first element of the $array return this position
48
+ // If $value is the first element of the $array return this position
52
49
if ($ arr [0 ] === $ value ) {
53
50
return 0 ;
54
51
}
55
52
56
- // Find range for binary search
53
+ // Find range for binary search
57
54
$ i = 1 ;
58
55
$ length = count ($ arr );
59
56
while ($ i < $ length && $ arr [$ i ] <= $ value ) {
0 commit comments