PHP four common basic sorting algorithms (with examples)

  
1. Analysis of bubble sorting ideas: In the set of numbers to be sorted, compare and adjust the adjacent numbers to the sequences that are not yet arranged, so that the larger numbers are sinking. , the smaller ones go up. That is, whenever two adjacent numbers are compared and found to have their ordering and sorting requirements reversed, they are interchanged. Code implementation: $arr=array(1,43,54,62,21,66,32,78,36,76,39);function bubbleSort($arr){$len=count($arr);//The Layer loop control requires the number of rounds of bubbling for($i=1;$i<$len;$i++){ //This layer loop is used to control the number of times each round needs to be compared for 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 the sorting code implementation: function selectSort($arr) {//Double loop completion, Number of outer control rounds, inner control comparison count $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 if($arr[$p] > $ Arr[$j]) {//Compare, find smaller, record the position of the minimum; and use the known minimum for comparison in the next comparison. $p = $j;}}//The position of the current minimum has been determined and saved to $p. If the position of the minimum value is found to be different from the currently assumed position $i, the position is interchangeable. If($p != $i) {$tmp = $arr[$p];$arr[$p] = $arr[$i];$arr[$i] = $tmp;}}//return to the final The result return $arr;}3. Insert sorting analysis: In the set of numbers to be sorted, assuming that the previous numbers are already in order, now insert the nth number into the preceding ordered number, so that The n numbers are also in order. This cycle is repeated until all are in order. Code implementation: function insertSort($arr) {$len=count($arr);for($i=1, $i<$len; $i++) {$tmp = $arr[$i];//Inner layer Loop control, compare and insert for($j=$i-1;$j>=0;$j--) {if($tmp < $arr[$j]) {//I found that the inserted element is small , swap position, swap the elements behind it with the previous element $arr[$j+1] = $arr[$j];$arr[$j] = $tmp;} else {//If you don't need it The moving elements, because they are already sorted, are arrays, so the previous ones do not need to be compared again. Break;}}}return $arr;}4. Quick Sorting Analysis: Select a datum element, usually the first element or the last element. Through a scan, the sequence to be sorted is divided into two parts, one part is smaller than the reference element, and a part is greater than or equal to the reference element. At this point, the reference element is in the correct position after it is sorted, and then the two parts of the division are recursively sorted in the same way. Code implementation: function quickSort($arr) {//First determine if you need to continue with $length = count($arr); if($length <= 1) {return $arr;}//Select the first element as The benchmark $base_num = $arr[0];//iterate through all the elements except the ruler, put them into two arrays according to the size relationship //initialize two arrays $left_array = array(); //less than the base $right_array = Array(); //greater than the base of for($i=1; $i<$length; $i++) {if($base_num > $arr[$i]) {//put the left array $left_array[] = $arr[$i];} else {//put $right_array[] = $arr[$i];}}//on the right side and then recursively call this function on the left and right arrays respectively. $left_array = quick_sort($left_array);$right_array = quick_sort($right_array);//Merge return array_merge($left_array, array($base_num), $right_array);} This article is from [System Home] www.xp85. Com
Copyright © Windows knowledge All Rights Reserved