Trying to code a first fit algorithm (explanation here). It doesn't output everything correctly at the end. There probably is an easier way to achieve this, but I need to do it using do and while statements.
From the output I only get the first array, none of the others display.
function first_fit(&$arr, &$max)
{
$bin = array(array(), array(), array(), array(), array(), array(), array(), array(), array() ,array() );
$i=0;
$j=0;
do
{
do
{
if (array_sum($bin[$j])+$arr[$i] <= $max) {$bin[$j][] = $arr[$i]; $k=1; }
$j++;
}
while($k = 0);
$i++;
$j=0;
}
while($i<=9);
outputarray($bin[0]);
outputarray($bin[1]);
outputarray($bin[2]);
outputarray($bin[3]);
outputarray($bin[4]);
outputarray($bin[5]);
outputarray($bin[6]);
outputarray($bin[7]);
outputarray($bin[8]);
outputarray($bin[9]);
}
The function outputarray() just prints the array in a formatted way.
Any help would be much appreciated.