3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
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