Php bubble sorting method

Source: Internet
Author: User
: This article mainly introduces the php bubble sorting method. if you are interested in the PHP Tutorial, refer to it. $ Arr = array (421,226,588 );

$ N = count ($ arr);/* count () function calculates the number of units in the array or the number of attributes in the object. For an array, return the number of its elements. for other values, return 1. If the parameter is a variable but the variable is not defined, 0 is returned. If the mode is set to COUNT_RECURSIVE (or 1), the number of elements in the array in the multi-dimensional array is calculated recursively */

// Echo $ n;

For ($ h = 0; $ h <$ n-1; $ h ++) {// Outer Loop n-1

For ($ I = 0; $ I <$ n-$ H-1; $ I ++ ){

If ($ arr [$ I]> $ arr [$ I + 1]) {// you can determine the array size and reverse the position.

$ K/p> $ Arr [$ I + 1] = $ arr [$ I];

$ Arr [$ I] = $ kong;

}

}

}

Var_dump ($ arr );

?>

Result:

Array (19) {[0] => int (2) [1] => int (4) [2] => int (5) [3] => int (9) [4] => int (10) [5] => int (11) [6] => int (16) [7] => int (21) [8] => int (23) [9] => int (26) [10] => int (39) [11] => int (44) [12] => int (55) [13] => int (55) [14] => int (85) [15] => int (88) [16] => int (226) [17] => int (421) [18] => int (588 )}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces the php bubble sorting method, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.