Another PHP-implemented Bubble Sorting Algorithm sharing _ php instance

Source: Internet
Author: User
This article mainly introduces another PHP-implemented Bubble sorting algorithm. The title also refers to several articles on this site that have already published Bubble sorting algorithms, if this does not meet your requirements, please refer to other implementation methods in the relevant articles. If you need it, you can refer to the classic bubble sort method, which has always been one of the methods used by many programs, in other words, the Bubble sorting method is more efficient than the PHP system function sort. This chapter does not discuss performance, so we will not use it to compare it with the system.

Bubble Sorting means to compare two adjacent numbers in sequence, then sort them by size until the last two digits. Because the sorting process always places decimal places forward and large numbers backward, it is equivalent to bubbles rising, so it is called Bubble sorting. But in fact, you can also use it based on your own needs. Place the big tree forward and place the decimal number backward.

<? Php/*** use the Bubble sorting method in PHP * // pre-declare an array $ arr = array (,); echo "original array "; print_r ($ arr); echo"
"; // Bubble sort function maopao ($ arr) {// traverse the first layer for ($ I = 0, $ k = count ($ arr ); $ I <$ k; $ I ++) {// perform Layer 2 traversal. Compare each element in the array with the outer element. // I + 1 indicates that the outer element traverses the for ($ j = $ I + 1; $ j <$ k; $ j ++) {// compare the two numbers of internal and external layers if ($ arr [$ I] <$ arr [$ j]) {// assign an array to the temporary variable $ temp = $ arr [$ j]; // The switching position $ arr [$ j] = $ arr [$ I]; // assign a value from the temporary variable $ arr [$ I] = $ temp ;}}// return the sorted array return $ arr ;} // print the sorted array echo 'sorted 'directly; print_r (maopao ($ arr);?>

Use the above code to execute the result

Original Array

The Code is as follows:

Array ([0] => 12 [1] => 45 [2] => 28 [3] => 30 [4] => 88 [5] => 67)


After sorting

The Code is as follows:

Array ([0] => 88 [1] => 67 [2] => 45 [3] => 30 [4] => 28 [5] => 12)


This is an example of the bubble method! There is no difficulty.

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.