3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $steps = 0; $arr = explode('|', '1|2|3|4|5'); reverse($arr); function quSort($input) { $input = array_values($input); $newArray = array($input[0]); array_shift($input); $end = $start = $newArray[0]; array_walk($input, function($num) use ($end, $start, &$newArray) { if ($num <= $start) { $start = $num; array_unshift($newArray, $num); } else if ($num >= $end) { $end = $num; $newArray[] = $num; } else { $end2 = count($newArray); $start2 = 0; $var = closer($num, $start, $end); if ($var === $end) { for($i = $end2-1; $i >= $start2; $i--) { if ($num > $newArray[$i] || $num === $newArray[$i]) { array_splice($newArray, $i, 0, array($num)); break 1; } } } else { for($i = $start2; $i < $end2; $i++) { if ($num < $newArray[$i] || $num === $newArray[$i]) { array_splice($newArray, $i, 0, array($num)); break 1; } } } } }); return $newArray; } function quickSort($input, &$steps) { $min = null; $max = null; $medium = null; if (count($input) === 2) { $input = array_values($input); if ($input[0] > $input[1]) { return array($input[1], $input[0]); } else { return array($input[0], $input[1]); } } else if (countOfNumbers($input) == 2) { $array = array(); $biggestNum = reset($input); foreach($input as $num) { if (empty($array)) { array_push($array, $num); } else { if ($biggestNum === $num) { array_push($array, $num); } else if ($biggestNum > $num) { array_unshift($array, $num); } else if ($num > $biggestNum) { array_push($array, $num); $biggestNum = $num; } } } return $array; } else if (!isRight($input, $min, $max, $medium)) { $middle = $medium; $left = array(); $right = array(); foreach ($input as $num) { if ($num > $middle) { array_push($right, $num); } else if ($num <= $middle) { array_push($left, $num); } } $arr = array(); if (count($left) > 0) { if (sameCheck($left)) { $arr = array_merge($arr, $left); } else { $steps++; $arr = array_merge($arr, quickSort($left, $steps)); } } if (count($right) > 0) { if (sameCheck($right)) { $arr = array_merge($arr, $right); } else { $steps++; $arr = array_merge($arr, quickSort($right, $steps)); } } return $arr; } else { return $input; } } function sameCheck ($input) { $oldInput = reset($input); foreach($input as $keys) { if ($oldInput !== $keys) { return false; } } return true; } function countOfNumbers($input) { $arr = array(); foreach ($input as $num) { if (!in_array($num, $arr)) { array_push($arr, $num); } } return count($arr); } function isRight ($input, &$min, &$max, &$medium) { $before = null; $check = true; foreach($input as $num) { if ($before === null) { $before = $num; $min = $num; $max = $num; $medium = $num; } if ($min > $num) { $min = $num; } else if ($max < $num) { $max = $num; } //echo implode('|', array($max, $min, $medium, $num)) . PHP_EOL; if ($num != $medium && $num < $max && $num > $min) { $medium = closer($max + $min, $medium, $num); } else if (($num != $medium && $medium === $min && $medium < $max)|| ($min === $max && $medium != $max)){ $medium = $max; } /*if () { $medium = $num; }*/ if ($num < $before) { $check = false; } $before = $num; } //$medium = ($max + $min) / 2/; return $check; } function closer($to, $medium, $new) { $to = floor($to / 2); if (abs($to - $medium) > abs($new - $to)) { return $new; } else { return $medium; } } /*$start = microtime(true); echo implode('|', quickSort($arr, $steps)) . PHP_EOL; echo $date - $start; */ $date = microtime(true); echo PHP_EOL; echo implode('|', $arr) . PHP_EOL; echo implode('|', quSort($arr)) . PHP_EOL; $date2 = microtime(true); echo $date2 - $date; echo PHP_EOL; echo implode('|', $arr) . PHP_EOL;; sort($arr); echo implode('|', $arr). PHP_EOL; $date3= microtime(true); echo $date3 - $date2; if (($date2 - $date) > ($date3 - $date2)) { echo PHP_EOL . 'still slower'; }
based on 89v5B
Output for 7.0.0 - 7.1.0
Fatal error: Uncaught Error: Call to undefined function reverse() in /in/XdreH:5 Stack trace: #0 {main} thrown in /in/XdreH on line 5
Process exited with code 255.
Output for 5.3.0 - 5.6.28
Fatal error: Call to undefined function reverse() in /in/XdreH on line 5
Process exited with code 255.