algorithm - sort numbers in php, -
hi using php learn algorithms, wanted convert psuedocode php,
for = 1 n − 1 minval = a[i] minindex = j = n if (a[j] < minval) minval = a[j] minindex = j exchange a[i] , a[minindex]
this corresponding code in php
$a = array(1, 4, 2, 3, 70, 10, 7 ); $n = sizeof($a); ($i = 0; $i == $n - 1; $i++){ ($j = $i + 1; $j == $n; $j++){ if ($a[$i] > $a[$j]){ $temp = $a[$j]; $a[$j] = $a[$i]; $a[$i] = $temp; } } } print_r($a);
print_r outputting array original order, why algorithms doents reorder array ?
you should check for
loops :
for ($i = 0; $i == $n - 1; $i++){ ($j = $i + 1; $j == $n; $j++){
should
for ($i = 0; $i < $n - 1; $i++){ ($j = $i + 1; $j < $n; $j++){
as second argument in for
requirement continue loop.
Comments
Post a Comment