PHP Bubble Sort instance sharing

Source: Internet
Author: User
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 1th round, compared 5 times, did 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 comparison summary: 1. Sort the 2nd round, compare 4 times, did not get from small to large sort 2. Bubbles out 8, next round don'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 3rd round, compare 3 times, did not get from small to large sort 2. Bubble out 6, the lower round don'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 4th round, compare 2 times, not get from small to large sort 2. Bubbles out 3, the lower round doesn't compare 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 5th round, compare 1 times, did not get from small to large sort 2. Bubbling out 2, because there is still a 1, do not compare, so by 5 round order, complete the whole sort.

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.

<?php function Order ($arr) {$count = count ($arr);       $temp = 0;                Outer control sort round for ($i =0; $i < $count-1; $i + +) {//inner control per-turn comparison 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); 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.