3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* 1 / \ 2 3 / \ \ 4 5 6 \ 7 */ $a = ['d'=>1, 'l'=>&$b, 'r'=>&$c]; $b = ['d'=>2, 'l'=>&$d, 'r'=>&$e]; $c = ['d'=>3, 'l'=>null, 'r'=>&$f]; $d = ['d'=>4, 'l'=>null, 'r'=>&$g]; $e = ['d'=>5, 'l'=>null, 'r'=>null]; $f = ['d'=>6, 'l'=>null, 'r'=>null]; $g = ['d'=>7, 'l'=>null, 'r'=>null]; bfs($a); echo "\n"; dfs($a); echo "\n"; function bfs($t) { $queue = [$t]; while ($queue) { $node = array_shift($queue); echo $node['d']; if (is_array($node['l'])) { array_push($queue, $node['l']); } if (is_array($node['r'])) { array_push($queue, $node['r']); } } } function dfs($t) { $stack = [$t]; while ($stack) { $node = array_pop($stack); echo $node['d']; if (is_array($node['r'])) { array_push($stack, $node['r']); } if (is_array($node['l'])) { array_push($stack, $node['l']); } } } $a = [5,1,2,8,3,4,1]; qsort($a); var_dump($a); function qsort(&$arr, $l = 0, $h = null) { if (is_null($h)) $h = count($arr) - 1; if ($l >= $h) return; $v = $arr[$l]; $i = $l; $j = $h; while ($i < $j) { while ($arr[$j] >= $v && $i < $j) { $j--; } while ($arr[$i] <= $v && $i < $j) { $i++; } if ($i == $j) { $arr[$l] = $arr[$i]; $arr[$i] = $v; break; } $tmp = $arr[$i]; $arr[$i] = $arr[$j]; $arr[$j] = $tmp; } qsort($arr, $l, $i - 1); qsort($arr, $i + 1, $h); } var_dump(binsearch($a, 0)); var_dump(binsearch($a, 1)); var_dump(binsearch($a, 6)); var_dump(binsearch($a, 8)); var_dump(binsearch($a, 9)); function binsearch($arr, $val) { $l = 0; $h = count($arr) - 1; while ($l <= $h) { $m = intval(($l + $h) / 2); if ($arr[$m] == $val) { return $m; } if ($arr[$m] < $val) { $l = $m + 1; } else { $h = $m - 1; } } return -1; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FTIfo
function name:  (null)
number of ops:  70
compiled vars:  !0 = $a, !1 = $b, !2 = $c, !3 = $d, !4 = $e, !5 = $f, !6 = $g
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   INIT_ARRAY                                       ~7      1, 'd'
          1        ADD_ARRAY_ELEMENT                                ~7      !1, 'l'
          2        ADD_ARRAY_ELEMENT                                ~7      !2, 'r'
          3        ASSIGN                                                   !0, ~7
   12     4        INIT_ARRAY                                       ~9      2, 'd'
          5        ADD_ARRAY_ELEMENT                                ~9      !3, 'l'
          6        ADD_ARRAY_ELEMENT                                ~9      !4, 'r'
          7        ASSIGN                                                   !1, ~9
   13     8        INIT_ARRAY                                       ~11     3, 'd'
          9        ADD_ARRAY_ELEMENT                                ~11     null, 'l'
         10        ADD_ARRAY_ELEMENT                                ~11     !5, 'r'
         11        ASSIGN                                                   !2, ~11
   14    12        INIT_ARRAY                                       ~13     4, 'd'
         13        ADD_ARRAY_ELEMENT                                ~13     null, 'l'
         14        ADD_ARRAY_ELEMENT                                ~13     !6, 'r'
         15        ASSIGN                                                   !3, ~13
   15    16        ASSIGN                                                   !4, <array>
   16    17        ASSIGN                                                   !5, <array>
   17    18        ASSIGN                                                   !6, <array>
   19    19        INIT_FCALL_BY_NAME                                       'bfs'
         20        SEND_VAR_EX                                              !0
         21        DO_FCALL                                      0          
   20    22        ECHO                                                     '%0A'
   21    23        INIT_FCALL_BY_NAME                                       'dfs'
         24        SEND_VAR_EX                                              !0
         25        DO_FCALL                                      0          
   22    26        ECHO                                                     '%0A'
   53    27        ASSIGN                                                   !0, <array>
   54    28        INIT_FCALL_BY_NAME                                       'qsort'
         29        SEND_VAR_EX                                              !0
         30        DO_FCALL                                      0          
   55    31        INIT_FCALL                                               'var_dump'
         32        SEND_VAR                                                 !0
         33        DO_ICALL                                                 
   84    34        INIT_FCALL                                               'var_dump'
         35        INIT_FCALL_BY_NAME                                       'binsearch'
         36        SEND_VAR_EX                                              !0
         37        SEND_VAL_EX                                              0
         38        DO_FCALL                                      0  $23     
         39        SEND_VAR                                                 $23
         40        DO_ICALL                                                 
   85    41        INIT_FCALL                                               'var_dump'
         42        INIT_FCALL_BY_NAME                                       'binsearch'
         43        SEND_VAR_EX                                              !0
         44        SEND_VAL_EX                                              1
         45        DO_FCALL                                      0  $25     
         46        SEND_VAR                                                 $25
         47        DO_ICALL                                                 
   86    48        INIT_FCALL                                               'var_dump'
         49        INIT_FCALL_BY_NAME                                       'binsearch'
         50        SEND_VAR_EX                                              !0
         51        SEND_VAL_EX                                              6
         52        DO_FCALL                                      0  $27     
         53        SEND_VAR                                                 $27
         54        DO_ICALL                                                 
   87    55        INIT_FCALL                                               'var_dump'
         56        INIT_FCALL_BY_NAME                                       'binsearch'
         57        SEND_VAR_EX                                              !0
         58        SEND_VAL_EX                                              8
         59        DO_FCALL                                      0  $29     
         60        SEND_VAR                                                 $29
         61        DO_ICALL                                                 
   88    62        INIT_FCALL                                               'var_dump'
         63        INIT_FCALL_BY_NAME                                       'binsearch'
         64        SEND_VAR_EX                                              !0
         65        SEND_VAL_EX                                              9
         66        DO_FCALL                                      0  $31     
         67        SEND_VAR                                                 $31
         68        DO_ICALL                                                 
  104    69      > RETURN                                                   1

Function bfs:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 4
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 18
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 26
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 4
Branch analysis from position: 27
Branch analysis from position: 4
Branch analysis from position: 26
Branch analysis from position: 18
filename:       /in/FTIfo
function name:  bfs
number of ops:  28
compiled vars:  !0 = $t, !1 = $queue, !2 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   25     1        INIT_ARRAY                                       ~3      !0
          2        ASSIGN                                                   !1, ~3
   26     3      > JMP                                                      ->26
   27     4    >   INIT_FCALL                                               'array_shift'
          5        SEND_REF                                                 !1
          6        DO_ICALL                                         $5      
          7        ASSIGN                                                   !2, $5
   28     8        FETCH_DIM_R                                      ~7      !2, 'd'
          9        ECHO                                                     ~7
   29    10        FETCH_DIM_R                                      ~8      !2, 'l'
         11        TYPE_CHECK                                  128          ~8
         12      > JMPZ                                                     ~9, ->18
   30    13    >   INIT_FCALL                                               'array_push'
         14        SEND_REF                                                 !1
         15        FETCH_DIM_R                                      ~10     !2, 'l'
         16        SEND_VAL                                                 ~10
         17        DO_ICALL                                                 
   32    18    >   FETCH_DIM_R                                      ~12     !2, 'r'
         19        TYPE_CHECK                                  128          ~12
         20      > JMPZ                                                     ~13, ->26
   33    21    >   INIT_FCALL                                               'array_push'
         22        SEND_REF                                                 !1
         23        FETCH_DIM_R                                      ~14     !2, 'r'
         24        SEND_VAL                                                 ~14
         25        DO_ICALL                                                 
   26    26    > > JMPNZ                                                    !1, ->4
   36    27    > > RETURN                                                   null

End of function bfs

Function dfs:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 4
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 18
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 26
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 4
Branch analysis from position: 27
Branch analysis from position: 4
Branch analysis from position: 26
Branch analysis from position: 18
filename:       /in/FTIfo
function name:  dfs
number of ops:  28
compiled vars:  !0 = $t, !1 = $stack, !2 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   RECV                                             !0      
   39     1        INIT_ARRAY                                       ~3      !0
          2        ASSIGN                                                   !1, ~3
   40     3      > JMP                                                      ->26
   41     4    >   INIT_FCALL                                               'array_pop'
          5        SEND_REF                                                 !1
          6        DO_ICALL                                         $5      
          7        ASSIGN                                                   !2, $5
   42     8        FETCH_DIM_R                                      ~7      !2, 'd'
          9        ECHO                                                     ~7
   43    10        FETCH_DIM_R                                      ~8      !2, 'r'
         11        TYPE_CHECK                                  128          ~8
         12      > JMPZ                                                     ~9, ->18
   44    13    >   INIT_FCALL                                               'array_push'
         14        SEND_REF                                                 !1
         15        FETCH_DIM_R                                      ~10     !2, 'r'
         16        SEND_VAL                                                 ~10
         17        DO_ICALL                                                 
   46    18    >   FETCH_DIM_R                                      ~12     !2, 'l'
         19        TYPE_CHECK                                  128          ~12
         20      > JMPZ                                                     ~13, ->26
   47    21    >   INIT_FCALL                                               'array_push'
         22        SEND_REF                                                 !1
         23        FETCH_DIM_R                                      ~14     !2, 'l'
         24        SEND_VAL                                                 ~14
         25        DO_ICALL                                                 
   40    26    > > JMPNZ                                                    !1, ->4
   50    27    > > RETURN                                                   null

End of function dfs

Function qsort:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 8
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 11
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 16
Branch analysis from position: 49
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
2 jumps found. (Code = 46) Position 1 = 21, Position 2 = 23
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 24, Position 2 = 17
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
2 jumps found. (Code = 46) Position 1 = 29, Position 2 = 31
Branch analysis from position: 29
2 jumps found. (Code = 44) Position 1 = 32, Position 2 = 25
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 40
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 49
Branch analysis from position: 49
Branch analysis from position: 40
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 16
Branch analysis from position: 49
Branch analysis from position: 16
Branch analysis from position: 25
2 jumps found. (Code = 46) Position 1 = 29, Position 2 = 31
Branch analysis from position: 29
Branch analysis from position: 31
Branch analysis from position: 31
Branch analysis from position: 17
2 jumps found. (Code = 46) Position 1 = 21, Position 2 = 23
Branch analysis from position: 21
Branch analysis from position: 23
Branch analysis from position: 23
Branch analysis from position: 8
filename:       /in/FTIfo
function name:  qsort
number of ops:  62
compiled vars:  !0 = $arr, !1 = $l, !2 = $h, !3 = $v, !4 = $i, !5 = $j, !6 = $tmp
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      0
          2        RECV_INIT                                        !2      null
   58     3        TYPE_CHECK                                    2          !2
          4      > JMPZ                                                     ~7, ->8
          5    >   COUNT                                            ~8      !0
          6        SUB                                              ~9      ~8, 1
          7        ASSIGN                                                   !2, ~9
   59     8    >   IS_SMALLER_OR_EQUAL                                      !2, !1
          9      > JMPZ                                                     ~11, ->11
         10    > > RETURN                                                   null
   61    11    >   FETCH_DIM_R                                      ~12     !0, !1
         12        ASSIGN                                                   !3, ~12
   62    13        ASSIGN                                                   !4, !1
   63    14        ASSIGN                                                   !5, !2
   64    15      > JMP                                                      ->47
   65    16    > > JMP                                                      ->18
   66    17    >   PRE_DEC                                                  !5
   65    18    >   FETCH_DIM_R                                      ~17     !0, !5
         19        IS_SMALLER_OR_EQUAL                              ~18     !3, ~17
         20      > JMPZ_EX                                          ~18     ~18, ->23
         21    >   IS_SMALLER                                       ~19     !4, !5
         22        BOOL                                             ~18     ~19
         23    > > JMPNZ                                                    ~18, ->17
   68    24    > > JMP                                                      ->26
   69    25    >   PRE_INC                                                  !4
   68    26    >   FETCH_DIM_R                                      ~21     !0, !4
         27        IS_SMALLER_OR_EQUAL                              ~22     ~21, !3
         28      > JMPZ_EX                                          ~22     ~22, ->31
         29    >   IS_SMALLER                                       ~23     !4, !5
         30        BOOL                                             ~22     ~23
         31    > > JMPNZ                                                    ~22, ->25
   71    32    >   IS_EQUAL                                                 !4, !5
         33      > JMPZ                                                     ~24, ->40
   72    34    >   FETCH_DIM_R                                      ~26     !0, !4
         35        ASSIGN_DIM                                               !0, !1
         36        OP_DATA                                                  ~26
   73    37        ASSIGN_DIM                                               !0, !4
         38        OP_DATA                                                  !3
   74    39      > JMP                                                      ->49
   76    40    >   FETCH_DIM_R                                      ~28     !0, !4
         41        ASSIGN                                                   !6, ~28
   77    42        FETCH_DIM_R                                      ~31     !0, !5
         43        ASSIGN_DIM                                               !0, !4
         44        OP_DATA                                                  ~31
   78    45        ASSIGN_DIM                                               !0, !5
         46        OP_DATA                                                  !6
   64    47    >   IS_SMALLER                                               !4, !5
         48      > JMPNZ                                                    ~33, ->16
   80    49    >   INIT_FCALL_BY_NAME                                       'qsort'
         50        SEND_VAR_EX                                              !0
         51        SEND_VAR_EX                                              !1
         52        SUB                                              ~34     !4, 1
         53        SEND_VAL_EX                                              ~34
         54        DO_FCALL                                      0          
   81    55        INIT_FCALL_BY_NAME                                       'qsort'
         56        SEND_VAR_EX                                              !0
         57        ADD                                              ~36     !4, 1
         58        SEND_VAL_EX                                              ~36
         59        SEND_VAR_EX                                              !2
         60        DO_FCALL                                      0          
   82    61      > RETURN                                                   null

End of function qsort

Function binsearch:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 7
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 15
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 21
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 7
Branch analysis from position: 25
Branch analysis from position: 7
filename:       /in/FTIfo
function name:  binsearch
number of ops:  27
compiled vars:  !0 = $arr, !1 = $val, !2 = $l, !3 = $h, !4 = $m
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   89     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   90     2        ASSIGN                                                   !2, 0
   91     3        COUNT                                            ~6      !0
          4        SUB                                              ~7      ~6, 1
          5        ASSIGN                                                   !3, ~7
   92     6      > JMP                                                      ->23
   93     7    >   ADD                                              ~9      !2, !3
          8        DIV                                              ~10     ~9, 2
          9        CAST                                          4  ~11     ~10
         10        ASSIGN                                                   !4, ~11
   94    11        FETCH_DIM_R                                      ~13     !0, !4
         12        IS_EQUAL                                                 !1, ~13
         13      > JMPZ                                                     ~14, ->15
   95    14    > > RETURN                                                   !4
   97    15    >   FETCH_DIM_R                                      ~15     !0, !4
         16        IS_SMALLER                                               ~15, !1
         17      > JMPZ                                                     ~16, ->21
   98    18    >   ADD                                              ~17     !4, 1
         19        ASSIGN                                                   !2, ~17
         20      > JMP                                                      ->23
  100    21    >   SUB                                              ~19     !4, 1
         22        ASSIGN                                                   !3, ~19
   92    23    >   IS_SMALLER_OR_EQUAL                                      !2, !3
         24      > JMPNZ                                                    ~21, ->7
  103    25    > > RETURN                                                   -1
  104    26*     > RETURN                                                   null

End of function binsearch

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.28 ms | 1416 KiB | 21 Q