= $size || $left > $right) { throw new Exception("index out of bounds"); } // Als de te controleren deellijst max 2 elementen bevat // Dan handmatig controleren if(($right - $left) < 2) { if($list[$left] === $value) return $left; if($list[$right] === $value) return $right; return -1; } //Bereken de middelste index van de deellijst en controleer //of $value voorkomt op die plaats $center = floor(($left + $right) / 2); if($list[$center] === $value) return $center; //Voer binary_search uit op de linkerhelft //En controleer of dat een resultaat heeft opgeleverd $result = binary_search($list, $value, $left, $center); if($result > -1) return $result; //Voer binary_search uit op de rechterhelft //Return het resultaat return binary_search($list, $value, $center, $right); } //Test: zoek 23 en 21 op in $list1 $list1 = array(2, 3, 5, 7, 9, 23, 57, 99); echo binary_search($list1, 23) . "\n"; echo binary_search($list1, 21);