3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = array(8,4,1,9,5,7,3,2,6,0); /** * ############################################################################ * The nitty gritty */ // This is it! Get the data to process, split it into halfs and compare // Returns: freshly sorted, reassembled array from split array-parts function mergesort($data) { // Only process if we're not down to one piece of data if(count($data)>1) { // Find out the middle of the current data set and split it there to obtain to halfs $data_middle = round(count($data)/2, 0, PHP_ROUND_HALF_DOWN); // and now for some recursive magic $data_part1 = mergesort(array_slice($data, 0, $data_middle)); $data_part2 = mergesort(array_slice($data, $data_middle, count($data))); // Setup counters so we can remember which piece of data in each half we're looking at $counter1 = $counter2 = 0; // iterate over all pieces of the currently processed array, compare size & reassemble for ($i=0; $i<count($data); $i++) { // if we're done processing one half, take the rest from the 2nd half if($counter1 == count($data_part1)) { $data[$i] = $data_part2[$counter2]; ++$counter2; // if we're done with the 2nd half as well or as long as pieces in the first half are still smaller than the 2nd half } elseif (($counter2 == count($data_part2)) or ($data_part1[$counter1] < $data_part2[$counter2])) { $data[$i] = $data_part1[$counter1]; ++$counter1; } else { $data[$i] = $data_part2[$counter2]; ++$counter2; } } } return $data; } // Initiate the recursive magic by calling the function once & print the output for our viewing pleasure $data = mergesort($data); print_r($data); ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/g748s
function name:  (null)
number of ops:  9
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   ASSIGN                                                   !0, <array>
   48     1        INIT_FCALL                                               'mergesort'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $2      
          4        ASSIGN                                                   !0, $2
   49     5        INIT_FCALL                                               'print_r'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                                 
   52     8      > RETURN                                                   1

Function mergesort:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 64
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 61
Branch analysis from position: 61
2 jumps found. (Code = 44) Position 1 = 64, Position 2 = 35
Branch analysis from position: 64
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 38, Position 2 = 43
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 60
Branch analysis from position: 60
2 jumps found. (Code = 44) Position 1 = 64, Position 2 = 35
Branch analysis from position: 64
Branch analysis from position: 35
Branch analysis from position: 43
2 jumps found. (Code = 47) Position 1 = 46, Position 2 = 50
Branch analysis from position: 46
2 jumps found. (Code = 43) Position 1 = 51, Position 2 = 56
Branch analysis from position: 51
1 jumps found. (Code = 42) Position 1 = 60
Branch analysis from position: 60
Branch analysis from position: 56
2 jumps found. (Code = 44) Position 1 = 64, Position 2 = 35
Branch analysis from position: 64
Branch analysis from position: 35
Branch analysis from position: 50
Branch analysis from position: 64
filename:       /in/g748s
function name:  mergesort
number of ops:  66
compiled vars:  !0 = $data, !1 = $data_middle, !2 = $data_part1, !3 = $data_part2, !4 = $counter1, !5 = $counter2, !6 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
   17     1        COUNT                                            ~7      !0
          2        IS_SMALLER                                               1, ~7
          3      > JMPZ                                                     ~8, ->64
   20     4    >   INIT_FCALL                                               'round'
          5        COUNT                                            ~9      !0
          6        DIV                                              ~10     ~9, 2
          7        SEND_VAL                                                 ~10
          8        SEND_VAL                                                 0
          9        SEND_VAL                                                 2
         10        DO_ICALL                                         $11     
         11        ASSIGN                                                   !1, $11
   22    12        INIT_FCALL_BY_NAME                                       'mergesort'
         13        INIT_FCALL                                               'array_slice'
         14        SEND_VAR                                                 !0
         15        SEND_VAL                                                 0
         16        SEND_VAR                                                 !1
         17        DO_ICALL                                         $13     
         18        SEND_VAR_NO_REF_EX                                       $13
         19        DO_FCALL                                      0  $14     
         20        ASSIGN                                                   !2, $14
   23    21        INIT_FCALL_BY_NAME                                       'mergesort'
         22        INIT_FCALL                                               'array_slice'
         23        SEND_VAR                                                 !0
         24        SEND_VAR                                                 !1
         25        COUNT                                            ~16     !0
         26        SEND_VAL                                                 ~16
         27        DO_ICALL                                         $17     
         28        SEND_VAR_NO_REF_EX                                       $17
         29        DO_FCALL                                      0  $18     
         30        ASSIGN                                                   !3, $18
   26    31        ASSIGN                                           ~20     !5, 0
         32        ASSIGN                                                   !4, ~20
   29    33        ASSIGN                                                   !6, 0
         34      > JMP                                                      ->61
   31    35    >   COUNT                                            ~23     !2
         36        IS_EQUAL                                                 !4, ~23
         37      > JMPZ                                                     ~24, ->43
   32    38    >   FETCH_DIM_R                                      ~26     !3, !5
         39        ASSIGN_DIM                                               !0, !6
         40        OP_DATA                                                  ~26
   33    41        PRE_INC                                                  !5
         42      > JMP                                                      ->60
   35    43    >   COUNT                                            ~28     !3
         44        IS_EQUAL                                         ~29     !5, ~28
         45      > JMPNZ_EX                                         ~29     ~29, ->50
         46    >   FETCH_DIM_R                                      ~30     !2, !4
         47        FETCH_DIM_R                                      ~31     !3, !5
         48        IS_SMALLER                                       ~32     ~30, ~31
         49        BOOL                                             ~29     ~32
         50    > > JMPZ                                                     ~29, ->56
   36    51    >   FETCH_DIM_R                                      ~34     !2, !4
         52        ASSIGN_DIM                                               !0, !6
         53        OP_DATA                                                  ~34
   37    54        PRE_INC                                                  !4
         55      > JMP                                                      ->60
   39    56    >   FETCH_DIM_R                                      ~37     !3, !5
         57        ASSIGN_DIM                                               !0, !6
         58        OP_DATA                                                  ~37
   40    59        PRE_INC                                                  !5
   29    60    >   PRE_INC                                                  !6
         61    >   COUNT                                            ~40     !0
         62        IS_SMALLER                                               !6, ~40
         63      > JMPNZ                                                    ~41, ->35
   44    64    > > RETURN                                                   !0
   45    65*     > RETURN                                                   null

End of function mergesort

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
175.49 ms | 1406 KiB | 21 Q