# PHP bubble sort algorithm

**WhatsUp Gold**.

**Not end users.**

**The basic concept of **

The basic concept of bubble sort is: comparison of two adjacent sequence number, The decimal is placed in front of, A large number of behind. Firstly, compared with first and second number, The decimal place before, The large number of placed after. Then compare the number second and number third, The decimal place before, The large number of placed after, So to continue, Until the last two numbers, The decimal place before, The large number of placed after. Repeat the above process, Still from the beginning of the first log comparison (because it may due to the exchange of second number and third number, The first number is not greater than the number second), The decimal place before, The large number of placed after, Has been compared to a pair of adjacent the decimal number before, The decimal place before, The large number of placed after, Second times over, Get a new minimum in the countdown number second. And so on, Until the final ranking.

Because of the sorting process is always decimal put forth, A large number of back, The equivalent of bubbles rising, So called bubble sort.

Using double loop, Outside the loop variable is set to I, Inner loop variable is set to j. The outer loop is repeated 9 times, Internal circulation repeat 9, 8, …, 1 times. The two elements of each comparison are associated with internal circulation J, They can be respectively by a[j] and the a[j+1] logo, The value of I were 1,2, &hellip, 9;, For each I, J values were 1,2, … 10-i. ** In many programming in **, We need a sequence sorting, In order to facilitate the statistics, Common sorting methods a bubble sort, Two binary tree sort, Selection sort etc. Bubble sort had been due to its simple method and high efficiency of popular.

**sorting process**

Imagine being a sorted array R (1..N) vertical, Each data element as the weight of the bubble, According to the light bubbles can not be in the bubble under the principle of, From lower to upper scanning array R, All scans to violate the principle of light bubble, The upward floating &rdquo ”, Such repeated, Until the end of any two bubbles are light on, Even in so far.

Sort the process is as follows:

49 38 65 97 76 13 27

38 49 65 97 76 13 27 & nbsp; comparison; the first and second numbers, Put the small front, Behind the large; 38 49 65 97 76 13 27 nbsp; between second and third the number of, Put the small front, Behind the large; 38 49 65 97 76 13 27 nbsp; between third and fourth the number of, Put the small front, Behind the large; 38 49 65 76 97 13 27 nbsp; between fourth and fifth the number of, Put the small front, Behind the large; 38 49 65 76 13 97 27 nbsp; between fifth and sixth the number of, Put the small front, Behind the large; 38 49 65 76 13 27 97 nbsp; between sixth and seventh the number of, Put the small front, Behind the big; so far, The first time of completion of the comparison, Get the following order: 38 49 65 76 13 27 97

And then according to the first trip to compare sorting, Until the completion of sequencing.

$arr = array(345,4,17,6,52,16,58,69,32,8,234); for($i=1;$i<count($arr);$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; } } }

**Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**.

**Free Download**

Posted by Adrian at October 23, 2013 - 8:15 PM