Fast sorting is commonly used in sorting, and efficiency is said to be good, and it uses a divide-and-conquer algorithm to achieve
Divide a large sequence into two smaller sequences that need to be sorted! How to divide, you need to find an element from the sequence as a reference element, it is common practice to take the first element as a reference element. When a sequence has only one element or 0 elements, it indicates that the sequence is sorted.
Steps:
1, using an element as the reference element
2, splits the sequence, forms the element which is less than the reference element and is greater than the reference element
3, respectively to the campus and more than two sequences using the same sorting algorithm to complete the sort (recursive call)
4, combine left, reference, and right elements to
<?php/** * Quick Sort * php Version 5.3.13 */header ("content-type:text/html;charset=utf-8"); function quicksort ($list {$len = count ($list);//define the recursive exit if ($len <= 1) {return $list;} $one = $list [0];//defines two sequences $left = $right = Array (); for ($i =1; $i < $len; + + $i) {if ($list [$i] < $one) {$left [] = $list [$i];} else{$right [] = $list [$i];}} $left = Quicksort ($left); $right = Quicksort ($right); $result = Array_merge ($left, Array ($one), $right); return $result;} $arr = Array (45,10,20,30,40,50,60); Echo ' before sort: ', Print_r ($arr); Echo ' In the process of running the program, because careless small wrong piece of code, and produce an error, let me feel the seriousness of the importance of the program, any of his happen we have to monitor.
Notice: Undefined offset:0 in D:\lcc\quicksort.php on line 10
PHP Quick Sort