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 forloops :

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

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -