The sorting methods used in the array are: bubble sort, select sort, insert sort, (fast sort).
1, Bubble sorting method (Exchange sorting method): Its basic idea is to treat the sort sequence from the back forward (starting from the larger elements of the subscript), and then compare the adjacent elements of the sorting code, if the reverse is found to exchange, so that the smaller sort code elements gradually moved from the back to the front, like the bubble gradually upward.
To illustrate:
<?PHP$arr=Array(0,-1,3,-5,2,10,5); //Sort by ascending order, from small to large $count=Count($arr); $temp=0; for($i= 0;$i<$count-1;$i++){ for($j= 0;$j<$count-$i;$j++){ //If the previous number is larger than the following, swap if($arr[$j]>$arr[$j+1]){ $temp=$arr[$j]; $arr[$j]=$arr[$j+1]; $arr[$j+1]=$temp; } } } Print_r($arr); ?>
PrintOut: Array ([0] =-5 [1] =-1 [2] = 0 [3] = 2 [4] = 3 [5] = 5 [6] + 10)
If the above sort is encapsulated as a function, the code is as follows:
<?PHPfunctionBubblesort (&$arr){ $count=Count($arr); $temp=0; for($i= 0;$i<$count-1;$i++){ for($j= 0;$j<$count-$i;$j++){ //If the previous number is larger than the following, swap if($arr[$j]>$arr[$j+1]){ $temp=$arr[$j]; $arr[$j]=$arr[$j+1]; $arr[$j+1]=$temp; } } } }$arr=Array(0,-1,3,-5,2,10,5); Bubblesort ($arr);Print_r($arr); ?>
Note: You need to add "&" to the arguments in the function before the arguments are passed by reference (address) so that you can change the value of the $arr, otherwise the final printout or the original array (that is, not sorted).
2. Select the Sorting method
The selection sort is an internal sorting method, which selects an element from the data to be sorted by the specified rules, undergoes a reorganization with other elements, and then switches the position according to the principle to achieve the order.
To illustrate:
<?PHP/*Select Sort*/ functionSelectsort (&$arr){ $temp=0; for($i= 0;$i<Count($arr)-1;$i++){ $minVal=$arr[$i]; $minIndex=$i; for($j=$i+1;$j<Count($arr);$j++){ if($minVal>$arr[$j]){ $minVal=$arr[$j]; $minIndex=$j; } } $temp=$arr[$i]; $arr[$i]=$arr[$minIndex]; $arr[$minIndex]=$temp; } } $arr=Array(2,0,-1,1); Selectsort ($arr); Print_r($arr); ?>
3. Insert Sort method
The basic idea is: to see the n order of the elements to be sorted into an ordered table and an unordered table, the beginning of the ordered table contains only one element, the unordered table contains n-1 elements, the ordering process every time from the unordered table to take the first element, his sorting code sequentially and ordered table elements of the sorting code to compare, Insert it into the appropriate position in the ordered table to make it a new ordered table.
To illustrate:
<?PHP//Insert Sort (ascending) functionInsertsort (&$arr){ //The default subscript is 0, this number is already ordered for($i= 1;$i<Count($arr);$i++){ //$insertVal is the number that is ready to be inserted $insertVal=$arr[$i]; //prepare to compare with $insertindex first $insertIndex=$i-1; //If this condition is met, the description has not been found in the appropriate location while($insertIndex>=0&&$insertVal<$arr[$insertIndex]){ $insertIndex--; } //Insert (then give $insertval the appropriate location) $arr[$insertIndex+1]=$insertVal; } } $arr=Array(2,0,-1,1); Insertsort ($arr); Print_r($arr); ?>
PHP Exercise II