Several examples of PHP implementation of bubble sorting

Source: Internet
Author: User

The basic concepts of BubbleSort are as follows:

Compare two adjacent numbers in sequence, place decimal places in front, and place large numbers in the back. That is, in the first step: first compare the number of 1st and the number of 2nd, and put the decimal number before and after the large number. Then compare the numbers of 2nd and 3rd, place the decimal places before and after the large number, and continue until the last two digits are compared. Place the decimal places before and after the large number. So far, the first stop is over, and the maximum number is placed at the end. In the second round: The comparison starts from the first logarithm (because the number of 2nd is exchanged with the number of 3rd, the number of 1st is no less than 2nd). Before and after placing the decimal number, always compare to the second to the last (the largest position in the last), and the second stop, get a new maximum number at the penultimate position (in fact, it is the second largest number in the entire series ). In this case, repeat the above process until the sorting is completed.

Example 1: Simple bubble sorting

The code is as follows: Copy code

<? Php
# Bubble sorting
$ Arr = array (28,112, 20,434, 65,765, 33,553, 45,423 );
$ Tmp;
For ($ I = 0; $ I <count ($ arr)-1; $ I ++ ){
For ($ j = 0; $ j <count ($ arr)-1-$ I; $ j ++ ){
If ($ arr [$ j]> $ arr [$ j + 1]) {
$ Tmp = $ arr [$ j];
$ Arr [$ j] = $ arr [$ j + 1];
$ Arr [$ j + 1] = $ tmp;
        }
    }
}

Print_r ($ arr );

Example 2

The code is as follows: Copy code

<? Php
   
// Function bubblesort (& $ arr ){
// Define a variable to save the exchanged value $ temp = 0;
For ($ I = 0; $ I <count ($ arr); $ I ++ ){
           
For ($ j = 0; $ j <count ($ arr)-$ I-1; $ j ++ ){
                       
If ($ arr [$ j]> $ arr [$ j + 1]) {
// If the number above is greater than the number next to it, they will switch $ temp = $ arr [$ j];
$ Arr [$ j] = $ arr [$ j + 1];
$ Arr [$ j + 1] = $ temp;
                }
            }           
        }
    }

$ Arr = array (, 99,-,-7 );
BubbleSort ($ arr );
Print_r ($ arr); // The array is used for value transfer. Therefore, an & symbol is used for address transfer during transmission to change the external variable.
?>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.