PHP Bubble Sorting Algorithm Implementation Code

Source: Internet
Author: User
The Bubble Sorting Algorithm is the most basic algorithm required by many computer learners. Today I found a lot of code on the Internet and found that there are c ++, Ruby, Java, and so on, that is, php is rarely found, so I wrote it myself.

The Bubble Sorting Algorithm is the most basic algorithm required by many computer learners. Today I found a lot of code on the Internet and found that there are c ++, Ruby, Java, and so on, that is, php is rarely found, so I wrote it myself.

The Code is as follows:


$ Arr = array (234 );
For ($ I = 1; $ I For ($ j = count ($ arr)-1; $ j >=$ I; $ j --){
If ($ arr [$ j] <$ arr [$ J-1]) {
$ Temp = $ arr [$ J-1];
$ Arr [$ J-1] = $ arr [$ j];
$ Arr [$ j] = $ temp;
}
}
}


Basic Concepts
The basic concept of Bubble Sorting is to compare two adjacent numbers in sequence, put decimal places in front, and put large numbers in the back. That is, first compare the numbers of 1st and 2nd, and put the decimal places before and after the large numbers. 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. Repeat the above process and compare it from the first logarithm (because of the exchange of 2nd numbers and 3rd numbers, the number of 1st is no longer greater than 2nd). In the website space, place the decimal number before, after a large number is placed, it is always compared to the adjacent number before the minimum number. Before the decimal number is placed, the large number is placed, and the second row ends. A new minimum number is obtained in the second to last number. So on until the sorting is completed.
Because the sorting process always places a decimal number forward and a large number backward, it is equivalent to bubble rising and server space, so it is called Bubble sorting.
It is implemented using a double loop. The External Loop Variable is set to I and the inner loop variable is set to j. The external cycle repeats 9 times, the internal cycle repeats 9 in sequence, the Hong Kong Server rental, 8 ,..., Once. The two elements for each comparison are related to the inner loop j. They can be identified by a [j] And a [j + 1] respectively. The values of I are 1, 2 ,..., 9. For each I, j values are 1, 2 ,... 10-i.

Generate
In many program designs, we need to sort a sequence to facilitate statistics. Common sorting methods include Bubble sorting, binary tree sorting, and selective sorting. Bubble Sorting has always been favored by its concise ideas and methods and high efficiency.

Sorting Process
Imagine the sorted array R [1 .. n] vertical erect, each data element is considered as a bubble with weight, according to the principle that light bubbles cannot be under heavy bubbles, scanning array R from bottom up, when a light bubble that violates this principle is scanned, it will "float" Up and forth until the last two bubbles are carried on by the light, and the severe bubbles are carried down.

Update: An error occurred while updating the Code.

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.