3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = [1,2,3,4,5,6,7,8,9]; heapsort($arr); var_dump($arr); $arr = [9,8,7,6,5,4,3,2,1]; heapsort($arr); var_dump($arr); $arr = [0,1,2,3,4,5,6,7,8,9,10,11,12,13]; var_dump(big5($arr)); function big5($arr) { $heapsize = count($arr); for ($i = intval($heapsize - 2) / 2; $i >= 0; $i--) { heapify($arr, $i, $heapsize); } for ($i = 0; $i < 5; $i++) { heapify($arr, $i, $heapsize); } return $arr[4]; } function heapsort(&$arr) { $heapsize = count($arr); // build heap for ($i = intval(($heapsize - 2) / 2); $i >= 0; $i--) { heapify($arr, $i, $heapsize); } // heap sort for ($heapsize = $heapsize - 1; $heapsize > 0; $heapsize--) { swap($arr[0], $arr[$heapsize]); heapify($arr, 0, $heapsize); } } function heapify(&$arr, $i, $heapsize) { while (true) { $l = 2 * $i + 1; $r = 2 * $i + 2; $largest = $i; if ($l < $heapsize && $arr[$largest] < $arr[$l]) { $largest = $l; } if ($r < $heapsize && $arr[$largest] < $arr[$r]) { $largest = $r; } if ($largest == $i) { break; } swap($arr[$i], $arr[$largest]); $i = $largest; } } function heapify_recursively(&$arr, $i, $heapsize) { $l = 2 * $i + 1; $r = 2 * $i + 2; $largest = $i; if ($l < $heapsize && $arr[$largest] < $arr[$l]) { $largest = $l; } if ($r < $heapsize && $arr[$largest] < $arr[$r]) { $largest = $r; } if ($largest != $i) { swap($arr[$i], $arr[$largest]); heapify($arr, $largest, $heapsize); } } function swap(&$a, &$b) { list($a, $b) = [$b, $a]; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7u44a
function name:  (null)
number of ops:  22
compiled vars:  !0 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
    3     1        INIT_FCALL_BY_NAME                                       'heapsort'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
    4     4        INIT_FCALL                                               'var_dump'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                                 
    6     7        ASSIGN                                                   !0, <array>
    7     8        INIT_FCALL_BY_NAME                                       'heapsort'
          9        SEND_VAR_EX                                              !0
         10        DO_FCALL                                      0          
    8    11        INIT_FCALL                                               'var_dump'
         12        SEND_VAR                                                 !0
         13        DO_ICALL                                                 
   10    14        ASSIGN                                                   !0, <array>
   11    15        INIT_FCALL                                               'var_dump'
         16        INIT_FCALL_BY_NAME                                       'big5'
         17        SEND_VAR_EX                                              !0
         18        DO_FCALL                                      0  $8      
         19        SEND_VAR                                                 $8
         20        DO_ICALL                                                 
   80    21      > RETURN                                                   1

Function big5:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 8
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
2 jumps found. (Code = 44) Position 1 = 26, Position 2 = 18
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
2 jumps found. (Code = 44) Position 1 = 26, Position 2 = 18
Branch analysis from position: 26
Branch analysis from position: 18
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 8
Branch analysis from position: 16
Branch analysis from position: 8
filename:       /in/7u44a
function name:  big5
number of ops:  29
compiled vars:  !0 = $arr, !1 = $heapsize, !2 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
   14     1        COUNT                                            ~3      !0
          2        ASSIGN                                                   !1, ~3
   15     3        SUB                                              ~5      !1, 2
          4        CAST                                          4  ~6      ~5
          5        DIV                                              ~7      ~6, 2
          6        ASSIGN                                                   !2, ~7
          7      > JMP                                                      ->14
   16     8    >   INIT_FCALL_BY_NAME                                       'heapify'
          9        SEND_VAR_EX                                              !0
         10        SEND_VAR_EX                                              !2
         11        SEND_VAR_EX                                              !1
         12        DO_FCALL                                      0          
   15    13        PRE_DEC                                                  !2
         14    >   IS_SMALLER_OR_EQUAL                                      0, !2
         15      > JMPNZ                                                    ~11, ->8
   18    16    >   ASSIGN                                                   !2, 0
         17      > JMP                                                      ->24
   19    18    >   INIT_FCALL_BY_NAME                                       'heapify'
         19        SEND_VAR_EX                                              !0
         20        SEND_VAR_EX                                              !2
         21        SEND_VAR_EX                                              !1
         22        DO_FCALL                                      0          
   18    23        PRE_INC                                                  !2
         24    >   IS_SMALLER                                               !2, 5
         25      > JMPNZ                                                    ~15, ->18
   21    26    >   FETCH_DIM_R                                      ~16     !0, 4
         27      > RETURN                                                   ~16
   22    28*     > RETURN                                                   null

End of function big5

Function heapsort:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 8
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
2 jumps found. (Code = 44) Position 1 = 35, Position 2 = 19
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
2 jumps found. (Code = 44) Position 1 = 35, Position 2 = 19
Branch analysis from position: 35
Branch analysis from position: 19
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 8
Branch analysis from position: 16
Branch analysis from position: 8
filename:       /in/7u44a
function name:  heapsort
number of ops:  36
compiled vars:  !0 = $arr, !1 = $heapsize, !2 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   25     1        COUNT                                            ~3      !0
          2        ASSIGN                                                   !1, ~3
   28     3        SUB                                              ~5      !1, 2
          4        DIV                                              ~6      ~5, 2
          5        CAST                                          4  ~7      ~6
          6        ASSIGN                                                   !2, ~7
          7      > JMP                                                      ->14
   29     8    >   INIT_FCALL_BY_NAME                                       'heapify'
          9        SEND_VAR_EX                                              !0
         10        SEND_VAR_EX                                              !2
         11        SEND_VAR_EX                                              !1
         12        DO_FCALL                                      0          
   28    13        PRE_DEC                                                  !2
         14    >   IS_SMALLER_OR_EQUAL                                      0, !2
         15      > JMPNZ                                                    ~11, ->8
   33    16    >   SUB                                              ~12     !1, 1
         17        ASSIGN                                                   !1, ~12
         18      > JMP                                                      ->33
   34    19    >   INIT_FCALL_BY_NAME                                       'swap'
         20        CHECK_FUNC_ARG                                           
         21        FETCH_DIM_FUNC_ARG                               $14     !0, 0
         22        SEND_FUNC_ARG                                            $14
         23        CHECK_FUNC_ARG                                           
         24        FETCH_DIM_FUNC_ARG                               $15     !0, !1
         25        SEND_FUNC_ARG                                            $15
         26        DO_FCALL                                      0          
   35    27        INIT_FCALL_BY_NAME                                       'heapify'
         28        SEND_VAR_EX                                              !0
         29        SEND_VAL_EX                                              0
         30        SEND_VAR_EX                                              !1
         31        DO_FCALL                                      0          
   33    32        PRE_DEC                                                  !1
         33    >   IS_SMALLER                                               0, !1
         34      > JMPNZ                                                    ~19, ->19
   37    35    > > RETURN                                                   null

End of function heapsort

Function heapify:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
2 jumps found. (Code = 44) Position 1 = 40, Position 2 = 4
Branch analysis from position: 40
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 46) Position 1 = 13, Position 2 = 17
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 19
Branch analysis from position: 18
2 jumps found. (Code = 46) Position 1 = 21, Position 2 = 25
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 27
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 30
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
Branch analysis from position: 30
2 jumps found. (Code = 44) Position 1 = 40, Position 2 = 4
Branch analysis from position: 40
Branch analysis from position: 4
Branch analysis from position: 27
Branch analysis from position: 25
Branch analysis from position: 19
Branch analysis from position: 17
filename:       /in/7u44a
function name:  heapify
number of ops:  41
compiled vars:  !0 = $arr, !1 = $i, !2 = $heapsize, !3 = $l, !4 = $r, !5 = $largest
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   40     3      > JMP                                                      ->39
   41     4    >   MUL                                              ~6      !1, 2
          5        ADD                                              ~7      ~6, 1
          6        ASSIGN                                                   !3, ~7
   42     7        MUL                                              ~9      !1, 2
          8        ADD                                              ~10     ~9, 2
          9        ASSIGN                                                   !4, ~10
   44    10        ASSIGN                                                   !5, !1
   45    11        IS_SMALLER                                       ~13     !3, !2
         12      > JMPZ_EX                                          ~13     ~13, ->17
         13    >   FETCH_DIM_R                                      ~14     !0, !5
         14        FETCH_DIM_R                                      ~15     !0, !3
         15        IS_SMALLER                                       ~16     ~14, ~15
         16        BOOL                                             ~13     ~16
         17    > > JMPZ                                                     ~13, ->19
   46    18    >   ASSIGN                                                   !5, !3
   48    19    >   IS_SMALLER                                       ~18     !4, !2
         20      > JMPZ_EX                                          ~18     ~18, ->25
         21    >   FETCH_DIM_R                                      ~19     !0, !5
         22        FETCH_DIM_R                                      ~20     !0, !4
         23        IS_SMALLER                                       ~21     ~19, ~20
         24        BOOL                                             ~18     ~21
         25    > > JMPZ                                                     ~18, ->27
   49    26    >   ASSIGN                                                   !5, !4
   52    27    >   IS_EQUAL                                                 !5, !1
         28      > JMPZ                                                     ~23, ->30
   53    29    > > JMP                                                      ->40
   55    30    >   INIT_FCALL_BY_NAME                                       'swap'
         31        CHECK_FUNC_ARG                                           
         32        FETCH_DIM_FUNC_ARG                               $24     !0, !1
         33        SEND_FUNC_ARG                                            $24
         34        CHECK_FUNC_ARG                                           
         35        FETCH_DIM_FUNC_ARG                               $25     !0, !5
         36        SEND_FUNC_ARG                                            $25
         37        DO_FCALL                                      0          
   56    38        ASSIGN                                                   !1, !5
   40    39    > > JMPNZ                                                    <true>, ->4
   58    40    > > RETURN                                                   null

End of function heapify

Function heapify_recursively:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 12, Position 2 = 16
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 18
Branch analysis from position: 17
2 jumps found. (Code = 46) Position 1 = 20, Position 2 = 24
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 26
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 41
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 41
Branch analysis from position: 26
Branch analysis from position: 24
Branch analysis from position: 18
Branch analysis from position: 16
filename:       /in/7u44a
function name:  heapify_recursively
number of ops:  42
compiled vars:  !0 = $arr, !1 = $i, !2 = $heapsize, !3 = $l, !4 = $r, !5 = $largest
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   61     3        MUL                                              ~6      !1, 2
          4        ADD                                              ~7      ~6, 1
          5        ASSIGN                                                   !3, ~7
   62     6        MUL                                              ~9      !1, 2
          7        ADD                                              ~10     ~9, 2
          8        ASSIGN                                                   !4, ~10
   64     9        ASSIGN                                                   !5, !1
   65    10        IS_SMALLER                                       ~13     !3, !2
         11      > JMPZ_EX                                          ~13     ~13, ->16
         12    >   FETCH_DIM_R                                      ~14     !0, !5
         13        FETCH_DIM_R                                      ~15     !0, !3
         14        IS_SMALLER                                       ~16     ~14, ~15
         15        BOOL                                             ~13     ~16
         16    > > JMPZ                                                     ~13, ->18
   66    17    >   ASSIGN                                                   !5, !3
   68    18    >   IS_SMALLER                                       ~18     !4, !2
         19      > JMPZ_EX                                          ~18     ~18, ->24
         20    >   FETCH_DIM_R                                      ~19     !0, !5
         21        FETCH_DIM_R                                      ~20     !0, !4
         22        IS_SMALLER                                       ~21     ~19, ~20
         23        BOOL                                             ~18     ~21
         24    > > JMPZ                                                     ~18, ->26
   69    25    >   ASSIGN                                                   !5, !4
   72    26    >   IS_NOT_EQUAL                                             !5, !1
         27      > JMPZ                                                     ~23, ->41
   73    28    >   INIT_FCALL_BY_NAME                                       'swap'
         29        CHECK_FUNC_ARG                                           
         30        FETCH_DIM_FUNC_ARG                               $24     !0, !1
         31        SEND_FUNC_ARG                                            $24
         32        CHECK_FUNC_ARG                                           
         33        FETCH_DIM_FUNC_ARG                               $25     !0, !5
         34        SEND_FUNC_ARG                                            $25
         35        DO_FCALL                                      0          
   74    36        INIT_FCALL                                               'heapify'
         37        SEND_REF                                                 !0
         38        SEND_VAR                                                 !5
         39        SEND_VAR                                                 !2
         40        DO_FCALL                                      0          
   76    41    > > RETURN                                                   null

End of function heapify_recursively

Function swap:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7u44a
function name:  swap
number of ops:  10
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   78     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   79     2        INIT_ARRAY                                       ~2      !1
          3        ADD_ARRAY_ELEMENT                                ~2      !0
          4        FETCH_LIST_R                                     $3      ~2, 0
          5        ASSIGN                                                   !0, $3
          6        FETCH_LIST_R                                     $5      ~2, 1
          7        ASSIGN                                                   !1, $5
          8        FREE                                                     ~2
   80     9      > RETURN                                                   null

End of function swap

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
173.78 ms | 1415 KiB | 16 Q