Algorithm and data structure is a programming staff of the internal strength, technical cattle are not cattle, generally will look at these two points. As a PHP programmer, upgrading your skills will certainly have to learn the algorithm.
Here are four entry-level sorting algorithms: bubble Sort, select sort, insert sort, quick sort.
First, bubble sort
Principle: For a set of data, compare the size of adjacent data, the value of small data in front, the value of large data is placed behind. (the following are arranged in ascending order, that is, from small to large)
Example: $arr = Array (6, 3, 8, 2, 9, 1);
$arr has 6 data, according to 22 comparison size as follows, pay attention to compare the number of rounds and the number of comparisons per round
First round sort:
Compare 6 and 3 for the first time comparing results:3 6 8 2 9 1
Second comparison 6 and 3 comparison results: 3 6 8 2 9 1
Third comparison 8 and 2 comparison results: 3 6 2 8 9 1
Fourth comparison 8 and 9 comparison results: 3 6 2 8 9 1
Fifth comparison 9 and 1 comparison results: 3 6 2 8 1 9
The first round of comparative summary: 1. Sort the 1 rounds, compare 5 times, not get from small to large sort 2. Because each comparison is a large number back, so the comparison is completed, you can be sure that the large number of rows at the end (9 has bubbled up, the next round can not be compared )
Second round sort:
Compare 3 and 6 for the first time comparing results:3 6 2 8 1 9
Second comparison 6 and 2 comparison results: 3 2 6 8 1 9
Third comparison 6 and 8 comparison results: 3 2 6 8 1 9
Fourth comparison 8 and 1 comparison results: 3 2 6 1 8 9
The second round of comparative summary: 1. Sort the first 2 rounds, compare 4 times, not get the order from small to large 2. Bubbles out of 8, the next round doesn't compare 8.
Third round sort:
Compare 3 and 2 for the first time comparing results:2 3 6 1 8 9
Second comparison 3 and 6 comparison results: 2 3 6 1 8 9
Third comparison 6 and 1 comparison results: 2 3 1 6 8 9
The third round of comparative summary: 1. Sort the first 3 rounds, compare 3 times, do not get from small to large sort 2. Bubbles out of the 6, the lower round doesn't compare 6.
Fourth round sort:
Compare 2 and 3 for the first time comparing results:2 3 1 6 8 9
Second comparison 3 and 1 comparison results: 2 1 3 6 8 9
The fourth round comparison summary: 1. Sort the first 4 rounds, compare 2 times, do not get from small to large sort 2. Bubbling out 3, not comparing 3.
Fifth round sort:
Compare 2 and 1 for the first time comparing results:1 2 3 6 8 9
The fifth round comparison summary: 1. Sort the first 5 rounds, compare 1 times, do not get from small to large sort 2. Bubble out 2, because there is still a 1, no longer compare, so by 5 round order, to complete the sorting.
Through the above five rounds of sequencing, several comparisons, we have reason to infer a conclusion:
For an array of length N, we need to sort the N-1 wheel, each I-wheel to compare n-i times. For this we can use the double loop, the outer loop to control the cycle round, the inner loop control the number of comparisons per wheel.
<?PHPfunctionOrder$arr) {$count=Count($arr); $temp= 0; //outer control sorting rounds for($i= 0;$i<$count-1;$i++) {//internal control of the number of comparisons per round for($j= 0;$j<$count-$i;$j++) {if($arr[$j] >$arr[$j+1]) {$temp=$arr[$j]; $arr[$j] =$arr[$j+1]; $arr[$j+1] =$temp; }}}return $arr; } $arr=Array(6,3,8,2,9,1);$res= Order ($arr);Var_dump($res);
Reference Link: http://www.cnblogs.com/shen-hua/p/5422676.html
Sort php Four-bubble sort