ggalan
10-05-2010, 02:21 AM
i am trying to write this algorithm in php but the output doesnt sort the array from small to large value like its supposed to. the algorithm is taken from a text book but my syntax might be wrong. can anyone lend a hand please
<?php
$arr = array( 4, 3, 2324, 28, 999, 821, 423, 22, 21, 2, 1 );
$j = 0;
for( $j; $j < $arr.length; $j++ )
{
$key = $arr[$j];
$i = $j - 1;
while( $i > -1 && $arr[$i] > $key )
{
$arr[$i + 1] = $arr[$i];
$i = $i - 1;
}
$arr[$i + 1] = $key;
}
print_r($arr);
?>
<?php
$arr = array( 4, 3, 2324, 28, 999, 821, 423, 22, 21, 2, 1 );
$j = 0;
for( $j; $j < $arr.length; $j++ )
{
$key = $arr[$j];
$i = $j - 1;
while( $i > -1 && $arr[$i] > $key )
{
$arr[$i + 1] = $arr[$i];
$i = $i - 1;
}
$arr[$i + 1] = $key;
}
print_r($arr);
?>