PHP bubble sorting algorithm _ PHP Tutorial

Source: Internet
Author: User
PHP bubble sorting algorithm. The basic concept of bubble sorting is to compare two adjacent numbers in sequence, put decimals in front, and put large numbers in the back. That is, first compare the number of 1st and 2nd, and put the decimal places before, large 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 and 3rd, the number of 1st is no longer greater than 2nd). before and after placing the decimal number, always compare to the adjacent number before the minimum number. Place the decimal number before the decimal number, place the large number, and end with the second row. 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 decimal places forward and large numbers backward, it is equivalent to bubbles rising, 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. External loops are repeated 9 times, and internal loops are repeated 9, 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 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.

The sorting process is as follows:
49 38 65 97 76 13 27
38 49 65 97 76 13 27 compare the number of 1st and 2nd, Small put the front side, large put behind 38 49 65 97 76 13 27 compare the number of 2nd and 3rd, small front side, large back side 38 49 65 97 76 13 27 Comparison 3rd and 4th number, small front side, the large side is 38 49 65 76 97 13 27 compared with 4th and 5th numbers. the small side is the front side, 38, 49, 65, 76, 13, 97, and 27 are larger than 5th and 6th, put the big back 38 49 65 76 13 27 97 compare the number of 6th and 7th, put the small front side, put the big back at this point, the first stop comparison, get the following order: 38 49 65 76 13 27 97
The comparison is continued based on the first sorting method until the sorting is completed.

 = (345,4,17,6,52,16,58,69,32,8,234(=1;<();++(=()-1;>=;--([]<[-1 = [-1[-1] = [[] = 

The basic concept of sort bubble sorting is to compare the adjacent two numbers in sequence, put the decimal points in front, and put the large numbers in the back. That is to say, first compare the number of 1st and 2nd, and put the decimal number before it is large...

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.