Business:
1. Quick Sort
Principle: Select a reference value in the initial sequence, compare the other numbers of the sequence with the value, put one side large on the other, and then treat the divided two array as a new sequence, re-select the reference value, and compare the division. Know that the number of elements in each array is 1 stops.
Code: At first glance, it looks like a recursive way to get better.
<? PHP
functionQuick_sort (&$list,$start,$count){ if($start>=$count-1)//set the termination condition. { return; } $i=$start; $j=$count-1; while($i!=$j) { while($list[$i] <=$list[$j]) { if($i==$j) { Break2; } $j--; } $tmp=$list[$i]; $list[$i] =$list[$j]; $list[$j] =$tmp; while($list[$i] <=$list[$j]) { if($i==$j) { Break2; } $i++; } $tmp=$list[$i]; $list[$i] =$list[$j]; $list[$j] =$tmp; } quick_sort ($list,$start,$j);//sort a new sequenceQuick_sort ($list,$j+1,$count);}//Test for($i= 0;$i< 10;$i++){ $list[] =Rand(2222,9999);}Var_dump($list);Echo"<br>";
Array (Ten) {[0]=> int (8807) [1]=> int (5257) [2]=> int (9920) [3]=> int (6181) [4]=> int (8825) [5]=> Int ( 9654) [6]=> Int (7439) [7]=> int (6529) [8]=> int (4514) [9]=> int (8810)}
Quick_sort ($list, 0,Count($list));Var_dump($list);
Array (Ten) {[0]=> int (4514) [1]=> int (5257) [2]=> int (6181) [3]=> int (6529) [4]=> int (7439) [5]=> Int ( 8807) [6]=> Int (8810) [7]=> int (8825) [8]=> int (9654) [9]=> int (9920)}?>
2. Heap Sequencing
Principle: Heap sequencing (heapsort) is a sort of sorting algorithm designed by using the data structure of heap tree (pile), which is a sort of selection. The elements of the specified index can be quickly positioned using the features of the array. The heap is divided into Dagen and small Gan, which are completely binary trees. The Dagen requirement is that the value of each node is not more than the value of its parent node, i.e. A[parent[i]] >= a[i]. In the non-descending order of the array, it is necessary to use the large root heap, because according to the requirements of Dagen, the maximum value must be at the top of the heap. (Excerpt from Baidu Encyclopedia ...) )
Plainly, this sort is based on the arrangement characteristics of a particular stacked tree. The operation of a simple point is that the original sequence into a large heap, the end-to-end interchange, remove the tail (at this time the tail is the largest, do not participate in the sort), and then into a large heap, and then swap ... Until the sequence is an element.
Code: There is a small problem, because the array starting from 0 bits, I did not do the processing ... Think not necessary, the main algorithm thought is the most important. So there's a lot of-1 processing. It's a little messy.
<?PHP/** Node Sieve operation, the root node and the child node comparison, if less than the sub-node is swapped value, and if the child nodes are not leaf nodes, then the child node also sieve operation. @param array pre-changed arrays @param int root node @param int max node return;*/functionShift (&$list,$i,$max _count){ if($max _count<= 0) { return; } $node= Floor($max _count/2); if(($list[$i-1] <$list[$i*2-1]) | | ($i* * <=$max _count&&$list[$i-1] <$list[$i* *])) { $change= ($i* * <=$max _count) && ($list[$i*2-1] <$list[$i*])?$i* *:$i*2-1; $tmp=$list[$i-1]; $list[$i-1] =$list[$change]; $list[$change] =$tmp; if($i* * <=$node) {Shift ($list,$i* *,$max _count); } if($i*2+1 <=$node) {Shift ($list,$i*2+1,$max _count); } } return ;}functionHeap_sort ($list){ $node= Floor(Count($list)/2); for($j=$node;$j>0;$j--) {Shift ($list,$j,Count($list)-1); } for($number=Count($list);$number> 1;) { $tmp=$list[0]; $list[0] =$list[$number-1]; $list[$number-1] =$tmp; $number--; $node= Floor($number/2); for($j=$node;$j>0;$j--) {Shift ($list,$j,$number-1); } } return $list; }//Test$list=Array(36,25,48,12,65,43,20,58);$list= Heap_sort ($list);Print_r($list);//Array ([0] = [1] = [2] = [3] = [4] [5] [6] + [7] = +/-+-[+] (+)--+----- 3 >?>
The idea of a sort of self-perception is still quite subtle. There is no change in the complexity of time, no matter what the sequence is, what the sieve does. Here sieve operation I use recursion, feeling is not particularly appropriate, read the book, the direct use of the loop to do, did not read too orz .... Take a look at ...
Algorithm exercises--quick sorting, heap sorting