It's easy to sort the numbers in PHP, and we can convert the strings into groups, for example:
$string = ‘94132768‘;
Pass
$arr = str_split(($string);
To group:
$arr = Array (9,4,1,3,2,7,6,8);
Sorting algorithms
The first kind we can sort by using PHP's built-in functions
Sort ($arr), Ascending, Rsort ($arr) Descending
The second is to sort using the selection sort method, which is about finding the smallest number from the array, then swapping it with the 1th digit, and so on:
9,4,1,3,2,7,6,8
9 and 1 Swap
1,4,9,3,2,7,6,8
Then the minimum number from 4 to 8 is 2, 2 and 4 are swapped
1,4,9,3,4,7,6,8
And so on
$n = count($arr);for($i=0;$i<$n;$i++){ $min = $arr[$i]; //比较[i,n)中的最小数,然后进行位置兑换 for($j=$i+1;$j<$n;$j++){ if($arr[$j] < $min) { $temp = $arr[$j]; $arr[$i] = $temp; $arr[$j] = $arr[$i]; $min = $arr[$j]; } }}print_r($arr);
You can try it all right. Very simple algorithm.
PHP Sorting algorithm: Select sort