Php bubble sorting, php bubble _ PHP Tutorial

Source: Internet
Author: User
Let's talk about php bubble sorting and php bubble sorting. Let's talk about php bubble sorting and talk about the code implemented by php bubble PHP: Copy the code as follows: functionbubble_sort ($ array) {for ($ i0; $ icount ($ array)-1; $ I ++) {$ I refers to php bubble sorting and php bubble sorting.

PHP implementation code is provided first:

The code is as follows:


Function bubble_sort ($ array ){
For ($ I = 0; $ I <count ($ array)-1; $ I ++) {// $ I indicates the number of elements that have been sorted
For ($ j = 0; $ j <count ($ array)-1-$ I; $ j ++) {// $ j indicates the number of elements to be sorted, subtract $ I from the total length.
If ($ array [$ j]> $ array [$ j + 1]) {// sort in ascending order
$ Temp = $ array [$ j];
$ Array [$ j] = $ array [$ j + 1];
$ Array [$ j + 1] = $ temp;
}
}
}
Return $ array;
}

$ A = array (5, 1, 4, 7 );

Code execution process:

The code is as follows:


I = 0;
J = 0;
If ($ arr [0]> $ arr [1]) => 5> 1. the condition is true and the position is switched to form a new array => 1 5 4 7 j ++
If ($ arr [1]> $ arr [2]) => 5> 4. the condition is true and the position is switched to form a new array => 1 4 5 7 j ++
If ($ arr [2]> $ arr [3]) => 5> 7 conditions are not true, and the array remains unchanged, 1 4 5 7 j ++ j = 3 exit the inner loop, I ++

And so on.

The code implemented by using PHP is provided as follows: function bubble_sort ($ array) {for ($ I = 0; $ I count ($ array)-1; $ I ++) {// $ I is already...

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.