PHP 91 98 91 bytes
edit #1: Empty $b
requires an addional condition in the curly braces (+7).
edit #2: minor golfings
edit #3: added second version
pretty straight forward. The nicest part is the ternary inside the array_shift
(which fails if you try it without the curlys)
function m($a,$b){for($c=[];$a|$b;)$c[]=array_shift(${$a&(!$b|$a[0]<$b[0])?a:b});return$c;}
or
function m($a,$b){for($c=[];$a|$b;)$c[]=array_shift(${$a?!$b|$a[0]<$b[0]?a:b:b});return$c;}
ungolfed
function m($a,$b)
{
$c=[];
while($a||$b)
{
$c[] = array_shift(${
$a&&(!$b||$a[0]<$b[0])
?a
:b
});
# echo '<br>', outA($a), ' / ', outA($b) , ' -> ', outA($c);
}
return $c;
}
test
$cases = array (
[1],[0,2,3,4], [0,1,2,3,4],
[1,5,10,17,19],[2,5,9,11,13,20], [1, 2, 5, 5, 9, 10, 11, 13, 17, 19, 20],
[1,2,3],[], [1,2,3],
[],[4,5,6], [4,5,6],
);
function outA($a) { return '['. implode(',',$a). ']'; }
echo '<table border=1><tr><th>A</th><th>B</th><th>expected</th><th>actual result</th></tr>';
while ($cases)
{
$a = array_shift($cases);
$b = array_shift($cases);
# echo '<hr>', outA($a), ' / ', outA($b) , ' -> ', outA($c);
$expect = array_shift($cases);
$result=m($a,$b);
echo '<tr><td>',outA($a),'</td><td>',outA($b),'</td><td>', outA($expect), '</td><td>', outA($result),'</td></tr>';
}
echo '</table>';