Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function bin_search($arr, $searchKey) {
- $left = 0;
- $right = count($arr);
- while($left < $right) {
- $middle = ($right + $left) / 2;
- if ($arr[$middle] == $searchKey) {
- $res = $middle;
- break;
- } elseif ($arr[$middle] > $searchKey) {
- $right = $middle;
- } else {
- $left = $middle + 1;
- }
- }
- return $res;
- }
- $exArr = array(1, 1, 2, 2, 3, 9, 11, 16);
- $result = bin_search($exArr, 3);
- print $result.PHP_EOL;
Advertisement
Add Comment
Please, Sign In to add comment