I have a homework to implement shannon-fano algorithm in language of my choice.So i choce PHP
here is my function with accepts array as paramentur and performs the shannon-fano algoritm
Code: Select all
function divide_array($array)
{
$sum=0;
$mid=array_sum($array)/2;
foreach($array as $k=>$v)
{
if($sum<$mid){
$sum=$sum+$array[$k];
$up[$k]=$array[$k];
$codeArr[$k]=0;
}
else
{
$down=array_slice($array,$k+1);
$codeArr[$k]=1;
}
}
divide_array($up);
divide_array($down);
echo "<pre>";
print_r($codeArr);
echo "</pre>";
}
whats the problem