1. Bubble algorithm, sorting algorithm, because in the sorting process is always a decimal place, the large number of backward, the equivalent of bubbles upward, so called bubble sort
functionMaopao_fun ($array){ $count=Count($array); if($count<= 1){ return $array; } for($i= 0;$i<$count;$i++){ for($j=$count-1;$j>$i;$j--){ if($array[$j] >$array[$j-1]){ $tmp=$array[$j]; $array[$j] =$array[$j-1]; $array[$j-1] =$tmp; } } } return $array;}
2. Quick Sort,
Quick Sort (Quicksort) is an improvement to the bubbling sort.
by C. A. R. Hoare was introduced in 1962. The basic idea is to divide the sorted data into two separate parts by a single trip,
All of the data in one part is smaller than any other part of the data, and then the two parts of the data are quickly sorted by this method,
The entire sorting process can be recursive, in order to achieve the entire data into an ordered sequence.
functionQuickSort ($arr){ $len=Count($arr); if($len<= 1){ return $arr; } $key=$arr[0]; $left _arr=Array(); $right _arr=Array(); for($i= 1;$i<$len;$i++){ if($arr[$i] <=$key){ $left _arr[] =$arr[$i]; }Else{ $right _arr[] =$arr[$i]; } } $left _arr= QuickSort ($left _arr); $right _arr= QuickSort ($right _arr); return Array_merge($left _arr,Array($key),$right _arr);}
3. Select sort
Each trip selects the smallest (or largest) element from the data element to be sorted,
The order is placed at the end of the ordered sequence, until all the data elements are sorted out. Select Sort is an unstable sort method
functionSelect_sort ($arr){ $count=Count($arr); for($i= 0;$i<$count;$i++){ for($j=$i+1;$j<$count;$j++){ if($arr[$i] >$arr[$j]){ $tmp=$arr[$i]; $arr[$i] =$arr[$j]; $arr[$j] =$tmp; } } } return $arr;}
4. Insert Sort
Starting with the first element, the element can be thought to have been sorted
Takes the next element and scans the sequence of elements that have been sorted from back to forward
If the element (sorted) is greater than the new element, move the element to the next position
functionInsert_sort ($arr){ $count=Count($arr); for($i= 1;$i<$count;$i++){ $tmp=$arr[$i]; $j=$i-1; while($arr[$j] >$tmp){ $arr[$j+1] =$arr[$j]; $arr[$j] =$tmp; $j--; } } return $arr;}
Classic sorting algorithm (PHP)