Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Hostgator 1 Penny

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


7,621 questions

10,674 answers

573 users

How to implement selection sort in PHP

1 Answer

0 votes
function selection_sort(array $arr)
{
    $len = sizeof($arr);
    for ($i = 0; $i < $len; $i++) {
        $minIndex = $i;
        $minValue = $arr[$i];
        for ($j = $i + 1; $j < $len; $j++) {
            if ($arr[$j] < $minValue) {
                $minIndex = $j;
                $minValue = $arr[$j];
            }
        }
        $arr[$minIndex]= $arr[$i];
        $arr[$i]= $minValue;
    }
    return $arr;
}

$arr = array();

for ($i = 0; $i < 20; $i++) 
    $arr[$i] = rand(1, 1000);

$result = selection_sort($arr);

foreach($result as $n) 
    echo $n . "</br>";
 
  
/*
run: 
 
44
66
73
189
202
355
526
546
578
594
601
736
744
746
750
827
834
881
993
999
   
*/ 

 




answered May 12 by avibootz
...