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"; traverse($t); 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']); } } } function traverse($t) { if (isset($t['d'])) { echo $t['d']; } if (is_array($t['l'])) { traverse($t['l']); } if (is_array($t['r'])) { traverse($t['r']); } } $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/oBVq8
function name:  (null)
number of ops:  74
compiled vars:  !0 = $a, !1 = $b, !2 = $c, !3 = $d, !4 = $e, !5 = $f, !6 = $g, !7 = $t
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   INIT_ARRAY                                       ~8      1, 'd'
          1        ADD_ARRAY_ELEMENT                                ~8      !1, 'l'
          2        ADD_ARRAY_ELEMENT                                ~8      !2, 'r'
          3        ASSIGN                                                   !0, ~8
   12     4        INIT_ARRAY                                       ~10     2, 'd'
          5        ADD_ARRAY_ELEMENT                                ~10     !3, 'l'
          6        ADD_ARRAY_ELEMENT                                ~10     !4, 'r'
          7        ASSIGN                                                   !1, ~10
   13     8        INIT_ARRAY                                       ~12     3, 'd'
          9        ADD_ARRAY_ELEMENT                                ~12     null, 'l'
         10        ADD_ARRAY_ELEMENT                                ~12     !5, 'r'
         11        ASSIGN                                                   !2, ~12
   14    12        INIT_ARRAY                                       ~14     4, 'd'
         13        ADD_ARRAY_ELEMENT                                ~14     null, 'l'
         14        ADD_ARRAY_ELEMENT                                ~14     !6, 'r'
         15        ASSIGN                                                   !3, ~14
   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'
   23    27        INIT_FCALL_BY_NAME                                       'traverse'
         28        SEND_VAR_EX                                              !7
         29        DO_FCALL                                      0          
   24    30        ECHO                                                     '%0A'
   67    31        ASSIGN                                                   !0, <array>
   68    32        INIT_FCALL_BY_NAME                                       'qsort'
         33        SEND_VAR_EX                                              !0
         34        DO_FCALL                                      0          
   69    35        INIT_FCALL                                               'var_dump'
         36        SEND_VAR                                                 !0
         37        DO_ICALL                                                 
   98    38        INIT_FCALL                                               'var_dump'
         39        INIT_FCALL_BY_NAME                                       'binsearch'
         40        SEND_VAR_EX                                              !0
         41        SEND_VAL_EX                                              0
         42        DO_FCALL                                      0  $25     
         43        SEND_VAR                                                 $25
         44        DO_ICALL                                                 
   99    45        INIT_FCALL                                               'var_dump'
         46        INIT_FCALL_BY_NAME                                       'binsearch'
         47        SEND_VAR_EX                                              !0
         48        SEND_VAL_EX                                              1
         49        DO_FCALL                                      0  $27     
         50        SEND_VAR                                                 $27
         51        DO_ICALL                                                 
  100    52        INIT_FCALL                                               'var_dump'
         53        INIT_FCALL_BY_NAME                                       'binsearch'
         54        SEND_VAR_EX                                              !0
         55        SEND_VAL_EX                                              6
         56        DO_FCALL                                      0  $29     
         57        SEND_VAR                                                 $29
         58        DO_ICALL                                                 
  101    59        INIT_FCALL                                               'var_dump'
         60        INIT_FCALL_BY_NAME                                       'binsearch'
         61        SEND_VAR_EX                                              !0
         62        SEND_VAL_EX                                              8
         63        DO_FCALL                                      0  $31     
         64        SEND_VAR                                                 $31
         65        DO_ICALL                                                 
  102    66        INIT_FCALL                                               'var_dump'
         67        INIT_FCALL_BY_NAME                                       'binsearch'
         68        SEND_VAR_EX                                              !0
         69        SEND_VAL_EX                                              9
         70        DO_FCALL                                      0  $33     
         71        SEND_VAR                                                 $33
         72        DO_ICALL                                                 
  118    73      > 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/oBVq8
function name:  bfs
number of ops:  28
compiled vars:  !0 = $t, !1 = $queue, !2 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
   27     1        INIT_ARRAY                                       ~3      !0
          2        ASSIGN                                                   !1, ~3
   28     3      > JMP                                                      ->26
   29     4    >   INIT_FCALL                                               'array_shift'
          5        SEND_REF                                                 !1
          6        DO_ICALL                                         $5      
          7        ASSIGN                                                   !2, $5
   30     8        FETCH_DIM_R                                      ~7      !2, 'd'
          9        ECHO                                                     ~7
   31    10        FETCH_DIM_R                                      ~8      !2, 'l'
         11        TYPE_CHECK                                  128          ~8
         12      > JMPZ                                                     ~9, ->18
   32    13    >   INIT_FCALL                                               'array_push'
         14        SEND_REF                                                 !1
         15        FETCH_DIM_R                                      ~10     !2, 'l'
         16        SEND_VAL                                                 ~10
         17        DO_ICALL                                                 
   34    18    >   FETCH_DIM_R                                      ~12     !2, 'r'
         19        TYPE_CHECK                                  128          ~12
         20      > JMPZ                                                     ~13, ->26
   35    21    >   INIT_FCALL                                               'array_push'
         22        SEND_REF                                                 !1
         23        FETCH_DIM_R                                      ~14     !2, 'r'
         24        SEND_VAL                                                 ~14
         25        DO_ICALL                                                 
   28    26    > > JMPNZ                                                    !1, ->4
   38    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/oBVq8
function name:  dfs
number of ops:  28
compiled vars:  !0 = $t, !1 = $stack, !2 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
   41     1        INIT_ARRAY                                       ~3      !0
          2        ASSIGN                                                   !1, ~3
   42     3      > JMP                                                      ->26
   43     4    >   INIT_FCALL                                               'array_pop'
          5        SEND_REF                                                 !1
          6        DO_ICALL                                         $5      
          7        ASSIGN                                                   !2, $5
   44     8        FETCH_DIM_R                                      ~7      !2, 'd'
          9        ECHO                                                     ~7
   45    10        FETCH_DIM_R                                      ~8      !2, 'r'
         11        TYPE_CHECK                                  128          ~8
         12      > JMPZ                                                     ~9, ->18
   46    13    >   INIT_FCALL                                               'array_push'
         14        SEND_REF                                                 !1
         15        FETCH_DIM_R                                      ~10     !2, 'r'
         16        SEND_VAL                                                 ~10
         17        DO_ICALL                                                 
   48    18    >   FETCH_DIM_R                                      ~12     !2, 'l'
         19        TYPE_CHECK                                  128          ~12
         20      > JMPZ                                                     ~13, ->26
   49    21    >   INIT_FCALL                                               'array_push'
         22        SEND_REF                                                 !1
         23        FETCH_DIM_R                                      ~14     !2, 'l'
         24        SEND_VAL                                                 ~14
         25        DO_ICALL                                                 
   42    26    > > JMPNZ                                                    !1, ->4
   52    27    > > RETURN                                                   null

End of function dfs

Function traverse:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 13
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 21
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
Branch analysis from position: 13
Branch analysis from position: 5
filename:       /in/oBVq8
function name:  traverse
number of ops:  22
compiled vars:  !0 = $t
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   RECV                                             !0      
   55     1        ISSET_ISEMPTY_DIM_OBJ                         0          !0, 'd'
          2      > JMPZ                                                     ~1, ->5
   56     3    >   FETCH_DIM_R                                      ~2      !0, 'd'
          4        ECHO                                                     ~2
   58     5    >   FETCH_DIM_R                                      ~3      !0, 'l'
          6        TYPE_CHECK                                  128          ~3
          7      > JMPZ                                                     ~4, ->13
   59     8    >   INIT_FCALL_BY_NAME                                       'traverse'
          9        CHECK_FUNC_ARG                                           
         10        FETCH_DIM_FUNC_ARG                               $5      !0, 'l'
         11        SEND_FUNC_ARG                                            $5
         12        DO_FCALL                                      0          
   61    13    >   FETCH_DIM_R                                      ~7      !0, 'r'
         14        TYPE_CHECK                                  128          ~7
         15      > JMPZ                                                     ~8, ->21
   62    16    >   INIT_FCALL_BY_NAME                                       'traverse'
         17        CHECK_FUNC_ARG                                           
         18        FETCH_DIM_FUNC_ARG                               $9      !0, 'r'
         19        SEND_FUNC_ARG                                            $9
         20        DO_FCALL                                      0          
   64    21    > > RETURN                                                   null

End of function traverse

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/oBVq8
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
-------------------------------------------------------------------------------------
   71     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      0
          2        RECV_INIT                                        !2      null
   72     3        TYPE_CHECK                                    2          !2
          4      > JMPZ                                                     ~7, ->8
          5    >   COUNT                                            ~8      !0
          6        SUB                                              ~9      ~8, 1
          7        ASSIGN                                                   !2, ~9
   73     8    >   IS_SMALLER_OR_EQUAL                                      !2, !1
          9      > JMPZ                                                     ~11, ->11
         10    > > RETURN                                                   null
   75    11    >   FETCH_DIM_R                                      ~12     !0, !1
         12        ASSIGN                                                   !3, ~12
   76    13        ASSIGN                                                   !4, !1
   77    14        ASSIGN                                                   !5, !2
   78    15      > JMP                                                      ->47
   79    16    > > JMP                                                      ->18
   80    17    >   PRE_DEC                                                  !5
   79    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
   82    24    > > JMP                                                      ->26
   83    25    >   PRE_INC                                                  !4
   82    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
   85    32    >   IS_EQUAL                                                 !4, !5
         33      > JMPZ                                                     ~24, ->40
   86    34    >   FETCH_DIM_R                                      ~26     !0, !4
         35        ASSIGN_DIM                                               !0, !1
         36        OP_DATA                                                  ~26
   87    37        ASSIGN_DIM                                               !0, !4
         38        OP_DATA                                                  !3
   88    39      > JMP                                                      ->49
   90    40    >   FETCH_DIM_R                                      ~28     !0, !4
         41        ASSIGN                                                   !6, ~28
   91    42        FETCH_DIM_R                                      ~31     !0, !5
         43        ASSIGN_DIM                                               !0, !4
         44        OP_DATA                                                  ~31
   92    45        ASSIGN_DIM                                               !0, !5
         46        OP_DATA                                                  !6
   78    47    >   IS_SMALLER                                               !4, !5
         48      > JMPNZ                                                    ~33, ->16
   94    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          
   95    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          
   96    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/oBVq8
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
-------------------------------------------------------------------------------------
  103     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  104     2        ASSIGN                                                   !2, 0
  105     3        COUNT                                            ~6      !0
          4        SUB                                              ~7      ~6, 1
          5        ASSIGN                                                   !3, ~7
  106     6      > JMP                                                      ->23
  107     7    >   ADD                                              ~9      !2, !3
          8        DIV                                              ~10     ~9, 2
          9        CAST                                          4  ~11     ~10
         10        ASSIGN                                                   !4, ~11
  108    11        FETCH_DIM_R                                      ~13     !0, !4
         12        IS_EQUAL                                                 !1, ~13
         13      > JMPZ                                                     ~14, ->15
  109    14    > > RETURN                                                   !4
  111    15    >   FETCH_DIM_R                                      ~15     !0, !4
         16        IS_SMALLER                                               ~15, !1
         17      > JMPZ                                                     ~16, ->21
  112    18    >   ADD                                              ~17     !4, 1
         19        ASSIGN                                                   !2, ~17
         20      > JMP                                                      ->23
  114    21    >   SUB                                              ~19     !4, 1
         22        ASSIGN                                                   !3, ~19
  106    23    >   IS_SMALLER_OR_EQUAL                                      !2, !3
         24      > JMPNZ                                                    ~21, ->7
  117    25    > > RETURN                                                   -1
  118    26*     > RETURN                                                   null

End of function binsearch

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
149.13 ms | 1425 KiB | 21 Q