1<?PHP2 //set the request header3 Header("Content-type:text/html;charset=utf-8");4 5 /*6 Choose the idea of sorting:7 For example: 10 7 2 6 is an array with the name arr8 requirements: Sort from small to large9 Ten 1th Round One arr[0] to arr[arr.length-1], select the minimum value, that is, 2, and arr[0] Exchange values A Draw 2 7 6 - - 2nd round the arr[1] to arr[arr.length-1], select the minimum value, that is, 6, and arr[1] Exchange values - Draw 2 6 7 - - 3rd round + arr[2] to arr[arr.length-1], select the minimum value, that is, 7, and arr[2] Exchange values - Draw 2 6 7 + A .... at Nth round - that the last value does not need to compare itself - - - Conclusion: - after each round of comparisons, the next round will not let arr[start] participate in the comparison in - */ to + - $arr=Array(10,7,2,6,3,1,5,6,7,3,-2,-2,-3); the $arr _count=Count($arr); * $ Var_dump($arr);Panax Notoginseng - //$arr _count-1 means that the last number doesn't have to be compared . the for($i= 0;$i<$arr _count-1;$i++){ + A $min=$arr[$i];//Minimum Value the $min _index=$i;//Subscript of the minimum value + - for($j=$i+1;$j<$arr _count;$j++){ $ $ if($min>$arr[$j]){ - $min=$arr[$j]; - $min _index=$j; the } - }Wuyi the if($min _index==$i){ - Continue; Wu } - About $temp=$arr[$i]; $ $arr[$i] =$min; - $arr[$min _index] =$temp; - - } A + Echo' ================================= '; the - Var_dump($arr); $ the the?>
Sort by Select Sort-php