This article mainly introduces another PHP-implemented Bubble sorting algorithm. The title also refers to several articles on this site that have already published Bubble sorting algorithms, if this does not meet your requirements, please refer to other implementation methods in the relevant articles. If you need it, you can refer to the classic bubble sort method, which has always been one of the methods used by many programs, in other words, the Bubble sorting method is more efficient than the PHP system function sort. This chapter does not discuss performance, so we will not use it to compare it with the system.
Bubble Sorting means to compare two adjacent numbers in sequence, then sort them by size until the last two digits. 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. But in fact, you can also use it based on your own needs. Place the big tree forward and place the decimal number backward.
<? Php/*** use the Bubble sorting method in PHP * // pre-declare an array $ arr = array (,); echo "original array "; print_r ($ arr); echo"
"; // Bubble sort function maopao ($ arr) {// traverse the first layer for ($ I = 0, $ k = count ($ arr ); $ I <$ k; $ I ++) {// perform Layer 2 traversal. Compare each element in the array with the outer element. // I + 1 indicates that the outer element traverses the for ($ j = $ I + 1; $ j <$ k; $ j ++) {// compare the two numbers of internal and external layers if ($ arr [$ I] <$ arr [$ j]) {// assign an array to the temporary variable $ temp = $ arr [$ j]; // The switching position $ arr [$ j] = $ arr [$ I]; // assign a value from the temporary variable $ arr [$ I] = $ temp ;}}// return the sorted array return $ arr ;} // print the sorted array echo 'sorted 'directly; print_r (maopao ($ arr);?>
Use the above code to execute the result
Original Array
The Code is as follows:
Array ([0] => 12 [1] => 45 [2] => 28 [3] => 30 [4] => 88 [5] => 67)
After sorting
The Code is as follows:
Array ([0] => 88 [1] => 67 [2] => 45 [3] => 30 [4] => 28 [5] => 12)
This is an example of the bubble method! There is no difficulty.