PHP simple example of Bubble sorting, php Bubble Sorting

Source: Internet
Author: User

PHP simple example of Bubble sorting, php Bubble Sorting

1. First, we must find out what is Bubble sorting. If we do not understand the principle of Bubble sorting, we cannot write code.

The basic concept of BubbleSort is to compare two adjacent numbers in sequence, put decimal places in front, and put 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.

PHP implementation code:

<? Php // Bubble sorting method 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 previous number is greater than the next one, then they exchange $ temp = $ arr [$ j]; $ arr [$ j] = $ arr [$ j + 1]; $ arr [$ j + 1] = $ temp ;}}$ arr = array (, 5,-,-7); bubbleSort ($ arr ); print_r ($ arr); // The array is a value transfer. Therefore, an ampersand (&) is used to transfer an address and change the external variable.

The simple example of the above PHP implementation of Bubble Sorting is all the content shared by xiaobian. I hope to give you a reference and support for the house of friends.

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.