3v4l.org

run code in 300+ PHP versions simultaneously
<?php $a = array(1,2,1,3,2,1,3,2,1,3,2,1,2,3,1,2,3); echo implode(" ", $a) . "\n"; $useSort = false; if ($useSort) { sort($a); } else { $first = 0; $last = sizeof($a) - 1; echo $last; for($i = $first; $i <= $last;) { echo $i . "\n"; $v = $a[$i]; echo "Found $v at index $i\n"; if ($v == 1) { echo "$v is low, swapping with value in index $first\n"; swap($a, $i, $first); $first += 1; } if ($v == 3) { echo "$v is high, swapping with value in index $last\n"; swap($a, $i, $last); $last -= 1; continue; } $i += 1; } } echo implode(" ", $a) . "\n"; function swap(&$arr, $k1, $k2) { echo "Swapping $k1 and $k2\n"; $tmp = $arr[$k2]; $arr[$k2] = $arr[$k1]; $arr[$k1] = $tmp; }
Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
1 2 1 3 2 1 3 2 1 3 2 1 2 3 1 2 3 160 Found 1 at index 0 1 is low, swapping with value in index 0 Swapping 0 and 0 1 Found 2 at index 1 2 Found 1 at index 2 1 is low, swapping with value in index 1 Swapping 2 and 1 3 Found 3 at index 3 3 is high, swapping with value in index 16 Swapping 3 and 16 3 Found 3 at index 3 3 is high, swapping with value in index 15 Swapping 3 and 15 3 Found 2 at index 3 4 Found 2 at index 4 5 Found 1 at index 5 1 is low, swapping with value in index 2 Swapping 5 and 2 6 Found 3 at index 6 3 is high, swapping with value in index 14 Swapping 6 and 14 6 Found 1 at index 6 1 is low, swapping with value in index 3 Swapping 6 and 3 7 Found 2 at index 7 8 Found 1 at index 8 1 is low, swapping with value in index 4 Swapping 8 and 4 9 Found 3 at index 9 3 is high, swapping with value in index 13 Swapping 9 and 13 9 Found 3 at index 9 3 is high, swapping with value in index 12 Swapping 9 and 12 9 Found 2 at index 9 10 Found 2 at index 10 11 Found 1 at index 11 1 is low, swapping with value in index 5 Swapping 11 and 5 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3

preferences:
360.67 ms | 408 KiB | 460 Q