PHP bubble algorithm and php bubble Algorithm

Source: Internet
Author: User

PHP bubble algorithm and php bubble Algorithm

I found that many companies have a bubble algorithm question during interviews. Generally, programmers can write this basic algorithm, but today I read it online, I found that the bubble Algorithm in many articles is wrong. It is not a bubble algorithm at all! What is the so-called resolution bubble algorithm? It really misleads new people. I will directly go to the Code. This is the bubble algorithm.


<?phpfunction bubbleSort($ary){$len = count($ary);for ($i=0; $i < $len - 1; $i++) { for ($j=0; $j < $len - 1 -$i; $j++) { if ($ary[$j] > $ary[$j + 1]) {$tmp = $ary[$j];$ary[$j] = $ary[$j + 1];$ary[$j + 1] = $tmp;}}}return $ary;}$ary = [7,6,789,3,56,8,1234,45];$newAry = bubbleSort($ary);print_r($newAry);?>


Well, let me talk about what is not a bubble algorithm.

Of course, we should first talk about what is the bubble algorithm, which is used to sort an out-of-order array in order of size. Let's take an example to illustrate the implementation principle.

$ary = [7,6,78,1];
Sort the array from small to large. First, compare the first number with the second number. In this array, compare 7 and 6, and 7 to 6, after the first loop, the array will look like this.

$ary = [6,7,78,1];
Compare 7 and 78 in the second loop, and the result remains unchanged.

$ary = [6,7,78,1];

For the third comparison between 78 and 1, 78 is larger than 1.

$ary = [6,7,1,78];
In this way, the internal cycle is completed once and the second cycle starts. I will not elaborate on the same as above.

This is the bubble algorithm. The name is very vivid, just like a small bubble.

However, I searched Baidu for the "bubble algorithm". The first article was this.

Classic sorting algorithm-Bubble sort

The Code he provided is as follows:

static void bubble_sort(int[] unsorted)        {            for (int i = 0; i < unsorted.Length; i++)            {                for (int j = i; j < unsorted.Length; j++)                {                    if (unsorted[i] > unsorted[j])                    {                        int temp = unsorted[i];                        unsorted[i] = unsorted[j];                        unsorted[j] = temp;                    }                }            }        }        static void Main(string[] args)        {            int[] x = { 6, 2, 4, 1, 5, 9 };            bubble_sort(x);            foreach (var item in x)            {                Console.WriteLine(item);            }            Console.ReadLine();        }


This is obviously not a bubble algorithm, okay ?!


This algorithm starts from the first number of an array and compares it with all the numbers next to it. If it is larger than the numbers next to it, it exchanges positions with each other, although the correct results can be obtained at the end, but this is not a bubble algorithm at all. It may be called another algorithm. It's really wrong.

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.