PHP bubble algorithm (recursive implementation)

Source: Internet
Author: User
This article shares a piece of code that uses recursion to implement the php bubble algorithm, and records the problems and solutions that have finally been used in the process. if you have any need, refer to the implementation below.

The code is as follows:


/*
Bubble algorithm (recursive implementation)
*/

Function maoPao ($ array, $ index = 0)
{
$ Count = count ($ array );
If ($ count-1) <= $ index)
Return $ array;

For ($ I = $ count-1; $ I> $ index; $ I --)
{
If ($ array [$ I] <$ array [$ i-1])
{
$ Tmp = $ array [$ I];
$ Array [$ I] = $ array [$ i-1];
$ Array [$ i-1] = $ tmp;
}
}
$ Index ++;
Return maoPao ($ array, $ index );
// Return maoPao ($ array, $ index ++ );
}

$ Arr = array (12, 4, 3, 1, 9, 5, 6, 8, 7 );
Var_dump (maoPao ($ arr ));

Result:

The code is as follows:


Array ([0] => 1 [1] => 3 [2] => 4 [3] => 5 [4] => 6 [5] => 7 [6] => 8 [7] => 9 [8] => 12)

Problem:

When trying this implementation, I encountered a problem that has not yet been solved.
Here:

The code is as follows:


$ Index ++;
Return maoPao ($ array, $ index );
// Return maoPao ($ array, $ index ++ );

/******************
If you use the third row directly, instead of $ index ++, and then ruturn, it will enter an endless loop. I output $ index at the beginning of the function, all of which are 0, that is to say, the parameter passed to the recursive function after $ index ++ is not the result of $ index ++ (that is, $ index = $ index + 1 ).
MaoPao ($ array, $ index ++) is not a brief description of $ index ++; return maoPao ($ array, $ index);. why are the two results different, I hope you can answer this question.
******************/

Supplement:

Answer:

The code is as follows:


The difference between $ index ++ and ++ $ index is that $ index ++ is called Post increment and ++ $ index is called pre increment, although the final $ index result will all be + 1. However, when variables are passed, they may be different.

$ Index = 1;
$ M = $ index ++;
Echo $ index .'
'; // The result is 2.
Echo $ m .'
'; // The result is 1. because it is post-increment, the initial $ index = 1 is first assigned to $ m, and then $ index is auto-incremented by 1;

$ Index = 1;
$ N = ++ $ index;
Echo $ index .'
'; // The result is 2.
Echo $ n; // The result is 2. because it is a previous increment, $ index + 1 is executed first, and then assigned to $ n;

This may not be easy to remember, so you must pay attention when using it. in the above problem, I ignored this problem and caused the infinite 0 value passed by $ index to lock the recursion.

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.