<?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 = true;
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
1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3
preferences:
253.38 ms | 406 KiB | 460 Q