Search
 
SCRIPT & CODE EXAMPLE
 

PHP

recursive binary search php

function binarySearch(Array $arr, $start, $end, $x){
    if ($end < $start)
        return false;
   
    $mid = floor(($end + $start)/2);
    if ($arr[$mid] == $x) 
        return true;
  
    elseif ($arr[$mid] > $x) {
  
        // call binarySearch on [start, mid - 1]
        return binarySearch($arr, $start, $mid - 1, $x);
    }
    else {
  
        // call binarySearch on [mid + 1, end]
        return binarySearch($arr, $mid + 1, $end, $x);
    }
}
  
// Driver code
$arr = array(1, 2, 3, 4, 5);
$value = 5;
if(binarySearch($arr, 0, count($arr) - 1, $value) == true) {
    echo $value." Exists";
}
else {
    echo $value." Doesnt Exist";
}
Comment

PREVIOUS NEXT
Code Example
Php :: kartik select 2 yii2 
Php :: add image php database 
Php :: php array insert before key 
Php :: distinct laravel not working 
Php :: php get char from string position 
Php :: autoload file laravel 
Php :: 413 error laravel 
Php :: custom autoload without composer php psr4 
Php :: php mail in localhost wamp 
Php :: current pathinfo php 
Php :: define constructor in trait php 
Php :: array sort php 
Php :: eloquent where raw 
Php :: trait php 
Php :: Genrate Random number in php 
Php :: php increment and decrement 
Php :: run laravel cron job on cpanel 
Php :: quitar guiones en string php 
Php :: php define class 
Php :: Uncaught ReferenceError: commonL10n is not defined 
Php :: windows logged in user name in php 
Php :: laravel post ajax proper csrf 
Php :: php undefined index meaNING 
Php :: access json with php 
Php :: acf sub_field image title 
Php :: Change date format on view - laravel 
Php :: laravel localization 
Php :: php if negative then 0 
Php :: laravel make factory 
Php :: eloquent insert 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =