## Fast sorting

**Realize the idea:**To mark the first element in turn, judge the following value in turn. If it is less than it, it is placed on the left, if it is larger than it, the right is placed, and the left and right two parts are regarded as a whole recursion, and then the array is spliced together.

Its optimal time complexity is O (nlogn), which is centered around marked elements and can be evenly distributed at a time and so that the worst time complexity is O (n^2).

function quickSort($arr) { $count = count($arr); //Statistics of the length of an arrayIf ($count < = 1) {/ / if the number is empty or 1, the original is returned to the array.Return $arr;}$index = $arr[0]; / / / / mark the first element as a mark$left = []; / / / / define a left empty array$right = []; / / / / define an empty arrayFor ($i = 1; $i)< $count; $i++) {/ / starting from the second number of the array and comparing with the first marker element.If ($arr[$i] < $index) {/ / if less than the first standard.Remember that elements are placed in the left array$left[] = $arr[$i];} else {/ / if it is greater than the first marked element, let it go.Into the right array$right[] = $arr[$i];}}$left = quickSort ($left); / / lefThe T array is treated as a new parameter, and then recursively called to perform the above sort.$right = quickSort ($right); / / right array is treated as a new parameter, and then recursively called to perform the above ordering.REturn array_merge ($left, [$arr[0]], $right); / / finally, each left array, tag element, and right array are spliced into a new array.}$arrtest=[12,43,54,33,23,14,44,53,10,3,56]; / / test array$res=quickSort ($arrtest);Var_dump ($res);

## Bubble sort

**Realize the idea:**Select a maximum value from array every time, always recursion.

Its optimal time complexity is O (n) [positive sequence, array in good condition], the worst time complexity is O (n^2) [reverse sequence: array sorting is the opposite.

function bubbleSort($arr) { $count = count($arr); //Statistics of the length of an arrayFor ($i = 1; $i < $count; $i++) {/ / control the number of wheels to be sorted; the example needs to compare the 10 round.For ($j = 0;$j < $count - $i; $j++) {/ / control the number of times required for each round to be compared, and the largest value in each round is chosen at the end.If ($arr[$j] > $arr[$j+1))) {$temp = $arr[$j]; / / through the $temp medium, put the big value in the back.$arr[$j] = $arr[$j+1];$arr[$j+1] = $temp;}}}Return $arr;/ / return the final result}$arrtest=[12,43,54,33,23,14,44,53,10,3,56]; / / test array$res=bubbleSort ($arrtest);Var_dump ($res);