Bubble sorting-php, bubble-php_PHP tutorial

Source: Internet
Author: User
Bubble sorting-php and bubble-php. Bubble sorting-php, bubble-php implementation code: functionbubble_sort ($ array) {for ($ i0; $ icount ($ array)-1; $ I ++) {$ I refers to the number of elements that have been sorted in ascending order-php, bubble-php
PHP implementation code is provided first:

Function bubble_sort ($ array) {for ($ I = 0; $ I <count ($ array)-1; $ I ++) {// $ I is 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. use the total length minus $ I if ($ array [$ j]> $ array [$ j + 1]). {// Sort by 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:
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 first: function bubble_sort ($ array) {for ($ I = 0; $ I count ($ array)-1; $ I ++) {// $ I indicates the number of elements that have already been sorted...

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.