<?PHP
$arr = Array (1, A, a, a, a, a, a.//1. Bubble sort//In the set of numbers to be sorted, for the currently not well-arranged sequence, from the back to the adjacent two numbers in turn to compare and adjust, so that the larger number to sink, smaller upward. That is, each time a comparison of two adjacent numbers finds that they are ordered in the opposite order as they are ordered, they are interchanged. functionBubblesort ($arr) { $len=Count($arr); //the layer loop controls the number of rounds that need to bubble for($i= 1;$i<$len;$i++) {//This layer loop is used to control how many times a number needs to be compared per round for($k= 0;$k<$len-$i;$k++) { if($arr[$k] >$arr[$k+ 1]) { $tmp=$arr[$k+ 1]; $arr[$k+ 1] =$arr[$k]; $arr[$k] =$tmp; } } } return $arr;}//2. Select sort//In the number of groups to sort, choose the smallest number to exchange with the number of the first position. Then in the remaining number, find the smallest and second position of the number of exchanges, so loop to the penultimate number and the last number comparison. functionSelectsort ($arr) { //double loop complete, outer control wheel count, inner control comparison number $len=Count($arr); for($i= 0;$i<$len-1;$i++) { //first assume the position of the smallest value $p=$i; for($j=$i+ 1;$j<$len;$j++) { //$arr [$p] is the currently known minimum value if($arr[$p] >$arr[$j]) { //Compare, find smaller, record the position of the minimum value, and compare with the known minimum value at the next comparison. $p=$j; } } //The location of the current minimum value has been determined and saved to $p. If the location of the minimum value is found to be different from the current assumed position $i, the position is interchangeable. if($p!=$i) { $tmp=$arr[$p]; $arr[$p] =$arr[$i]; $arr[$i] =$tmp; } } //return to final result return $arr;}//3. Insert sort//In the set of numbers to be sorted, assuming that the previous number is already in order, now the nth number is inserted into the ordinal number in front, so that the n number is also in the order. This cycle is repeated until all the rows are in order. functionInsertsort ($arr) { $len=Count($arr); for($i= 1;$i<$len;$i++) { $tmp=$arr[$i]; //Inner loop control, compare and insert for($j=$i-1;$j>= 0;$j--) { if($tmp<$arr[$j]) { //find the inserted element to be small, swap position, swap the element behind with the previous element $arr[$j+ 1] =$arr[$j]; $arr[$j] =$tmp; } Else { //If you encounter an element that does not need to be moved, because the array is already sorted, the preceding one does not need to be compared again. Break; } } } return $arr;}//4. Quick Sort//Select a datum element, usually select the first element or the last element. With a scan, the pending sequence is divided into two parts, some smaller than the Datum element, and a portion greater than or equal to the datum element. At this point the datum element is in the correct position after it is ordered, and then the same method is used to sort the two parts recursively. functionQuickSort ($arr) { //determine if you need to proceed $length=Count($arr); if($length<= 1) { return $arr; } //Select the first element as a datum $base _num=$arr[0]; //iterate through all elements except the ruler, put in two arrays by size relationship//Initialize two arrays $left _array=Array(); //less than the baseline $right _array=Array(); //greater than the baseline for($i= 1;$i<$length;$i++) { if($base _num>$arr[$i]) { //put in the left array $left _array[] =$arr[$i]; } Else { //put it on the right $right _array[] =$arr[$i]; } } //This function is recursively called on the left and right arrays by the same sort method. $left _array= Quick_sort ($left _array); $right _array= Quick_sort ($right _array); //Merging return Array_merge($left _array,Array($base _num),$right _array);}
Sort of php-4