3v4l.org

run code in 300+ PHP versions simultaneously
<?php echo "Practicing Binary Search <br>"; function printArray($arr){ foreach ($arr as $value) { echo $value.", "; # code... } echo "<br>"; } $list=array(3,6,5,7,13,10,1,8); //--------------------------------------------------------------------- //function echo "Binary Search, recursive method <br>"; function BSearch($num,$list){ sort($list); printArray($list); echo "<br>"; $left=0; $right=count($list)-1; echo "Number to search:".$num."<br>"; $key=floor(recursiveBSearch($num,$list,$left,$right)); echo "The key is ".$key; } function recursiveBSearch($num,$arr,$left,$right){ if($left>$right){ return -1; } $mid=($left+$right)/2; if($arr[$mid]==$num){ return $mid; } else if($arr[$mid]<$num){ return recursiveBSearch($num,$arr,$mid+1,$right); } else if($arr[$mid]>$num){ return recursiveBSearch($num,$arr,$left,$mid-1); } } //----------------------------------------------------------------------------- //calling function BSearch(5,$list); ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/h6i6p
function name:  (null)
number of ops:  8
compiled vars:  !0 = $list
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ECHO                                                     'Practicing+Binary+Search+%3Cbr%3E'
   14     1        ASSIGN                                                   !0, <array>
   20     2        ECHO                                                     'Binary+Search%2C+recursive+method+%3Cbr%3E'
   55     3        INIT_FCALL                                               'bsearch'
          4        SEND_VAL                                                 5
          5        SEND_VAR                                                 !0
          6        DO_FCALL                                      0          
   60     7      > RETURN                                                   1

Function printarray:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 6
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 6
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
filename:       /in/h6i6p
function name:  printArray
number of ops:  9
compiled vars:  !0 = $arr, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
    7     1      > FE_RESET_R                                       $2      !0, ->6
          2    > > FE_FETCH_R                                               $2, !1, ->6
    8     3    >   CONCAT                                           ~3      !1, '%2C+'
          4        ECHO                                                     ~3
    7     5      > JMP                                                      ->2
          6    >   FE_FREE                                                  $2
   11     7        ECHO                                                     '%3Cbr%3E'
   12     8      > RETURN                                                   null

End of function printarray

Function bsearch:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/h6i6p
function name:  BSearch
number of ops:  29
compiled vars:  !0 = $num, !1 = $list, !2 = $left, !3 = $right, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   24     2        INIT_FCALL                                               'sort'
          3        SEND_REF                                                 !1
          4        DO_ICALL                                                 
   25     5        INIT_FCALL                                               'printarray'
          6        SEND_VAR                                                 !1
          7        DO_FCALL                                      0          
   26     8        ECHO                                                     '%3Cbr%3E'
   27     9        ASSIGN                                                   !2, 0
   28    10        COUNT                                            ~8      !1
         11        SUB                                              ~9      ~8, 1
         12        ASSIGN                                                   !3, ~9
   29    13        CONCAT                                           ~11     'Number+to+search%3A', !0
         14        CONCAT                                           ~12     ~11, '%3Cbr%3E'
         15        ECHO                                                     ~12
   30    16        INIT_FCALL                                               'floor'
         17        INIT_FCALL_BY_NAME                                       'recursiveBSearch'
         18        SEND_VAR_EX                                              !0
         19        SEND_VAR_EX                                              !1
         20        SEND_VAR_EX                                              !2
         21        SEND_VAR_EX                                              !3
         22        DO_FCALL                                      0  $13     
         23        SEND_VAR                                                 $13
         24        DO_ICALL                                         $14     
         25        ASSIGN                                                   !4, $14
   31    26        CONCAT                                           ~16     'The+key+is+', !4
         27        ECHO                                                     ~16
   32    28      > RETURN                                                   null

End of function bsearch

Function recursivebsearch:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 15
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 27
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 38
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/h6i6p
function name:  recursiveBSearch
number of ops:  39
compiled vars:  !0 = $num, !1 = $arr, !2 = $left, !3 = $right, !4 = $mid
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
   36     4        IS_SMALLER                                               !3, !2
          5      > JMPZ                                                     ~5, ->7
   37     6    > > RETURN                                                   -1
   39     7    >   ADD                                              ~6      !2, !3
          8        DIV                                              ~7      ~6, 2
          9        ASSIGN                                                   !4, ~7
   40    10        FETCH_DIM_R                                      ~9      !1, !4
         11        IS_EQUAL                                                 !0, ~9
         12      > JMPZ                                                     ~10, ->15
   41    13    > > RETURN                                                   !4
         14*       JMP                                                      ->38
   43    15    >   FETCH_DIM_R                                      ~11     !1, !4
         16        IS_SMALLER                                               ~11, !0
         17      > JMPZ                                                     ~12, ->27
   44    18    >   INIT_FCALL_BY_NAME                                       'recursiveBSearch'
         19        SEND_VAR_EX                                              !0
         20        SEND_VAR_EX                                              !1
         21        ADD                                              ~13     !4, 1
         22        SEND_VAL_EX                                              ~13
         23        SEND_VAR_EX                                              !3
         24        DO_FCALL                                      0  $14     
         25      > RETURN                                                   $14
         26*       JMP                                                      ->38
   46    27    >   FETCH_DIM_R                                      ~15     !1, !4
         28        IS_SMALLER                                               !0, ~15
         29      > JMPZ                                                     ~16, ->38
   47    30    >   INIT_FCALL_BY_NAME                                       'recursiveBSearch'
         31        SEND_VAR_EX                                              !0
         32        SEND_VAR_EX                                              !1
         33        SEND_VAR_EX                                              !2
         34        SUB                                              ~17     !4, 1
         35        SEND_VAL_EX                                              ~17
         36        DO_FCALL                                      0  $18     
         37      > RETURN                                                   $18
   50    38    > > RETURN                                                   null

End of function recursivebsearch

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
172.18 ms | 1407 KiB | 19 Q