algorithm - Why does this porting of C to PHP of Bubble Sort do not work? -
i decided create helper files languages containing sorting algorithms ans searching algorithms. started c , wanted make adjacent in php. array passing not getting sorted. why?
<?php function bubblesort($array, $length) { for($i = 0; $i < $length - 1; ++$i) { for($j = 0; $j < $length - $i - 1; ++$j) { if($array[$j] > $array[j + 1]) { $tmp = $array[$j]; $array[j] = $array[j + 1]; $array[j + 1] = $tmp; } } } } $array = [5,2,3,1,2]; $size = 5; print "array now: "; for($i = 0; $i < $size; $i++) { print "{$array[$i]} "; } print "\n"; bubblesort($array, $size); for($i = 0; $i < $size; $i++) { print "{$array[$i]} "; } print "\n"; ?>
you not passing array reference, hence function has return array , have assign original array.
provided there no syntax errors otherwise, didn't at.
Comments
Post a Comment