This article mainly introduced the PHP sorting algorithm bubble sort (Bubble sort) Implementation method, referring to the big data structure of the algorithm, combined with the case form more detailed analysis of the bubble sorting principle and related implementation skills, the need for friends can refer to the next
In this paper, we describe the implementation method of the bubble ordering algorithm (Bubble sort) in PHP sorting. Share to everyone for your reference, as follows:
Basic idea:
Bubble sort is a sort of exchange, the basic idea is: 22 compare the keywords of adjacent records, if the reverse order is exchanged until there is no reverse order of records.
The simplest sort implementation:
Let's take a look at the sort methods that are often used before you learn the various sorting methods (at least I am ...). ):
The type hint array is used here, the unfamiliar or unaccustomed classmate can be removed without affecting the result of the Operation function Mysort (array & $arr) { $length = count ($arr); for ($i = 0; $i < $length-1, $i + +) { for ($j = $i + 1; $j < $length; $j + +) { //Put small keywords in front if ($arr [$i] > $a rr[$j]) { $temp = $arr [$i]; $arr [$i] = $arr [$j]; $arr [$j] = $temp; }}} } $arr = Array (9,1,5,8,3,7,4,6,2); Mysort ($arr);p rint_r ($arr);
The above code, strictly speaking, is not a standard bubble sort, because it does not satisfy the "22 compare adjacent records" bubble sort idea, it is simply a simple exchange sort. The idea is simply: start with the first keyword, compare each keyword with all the keywords behind it, and get the minimum value in Exchange. But this algorithm is very inefficient.
Bubble Sort algorithm:
It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted.
The name of this algorithm is because the larger elements in the array will gradually "sink" to the back of the array after sorting, and the smaller elements will gradually "float" to the front of the array, hence the name.
The bubbling Sort algorithm works as follows: (from back to forward)
1. Compare adjacent elements. If the first one is bigger than the second one, swap them both.
2. Do the same work for each pair of adjacent elements, starting from the first pair to the end of the last pair. At this point, the last element should be the maximum number.
3. Repeat the above steps for all elements except the last one.
4. Repeat the above steps each time for fewer elements until there are no pairs of numbers to compare.
Look at the text description is not necessarily understand, look at the code:
Interchange method function swap (array & $arr, $a, $b) { $temp = $arr [$a]; $arr [$a] = $arr [$b]; $arr [$b] = $temp;} Bubble sort function Bubblesort (array & $arr) { $length = count ($arr); for ($i = 0; $i < $length-1; $i + +) { //from back to front layer up the small element for ($j = $length-2; $j >= $i; $j-) { //22 compare adjacent Records if ($arr [$j] > $arr [$j + 1]) { swap ($arr, $j, $j + 1);}}}
Bubble Sorting Algorithm improvements:
"Big talk data structure" really is a good book, and also introduced the bubble sorting algorithm improvement, here I directly moved its statement:
Can the above bubble sorting algorithm also be optimized? The answer is yes. Imagine that if the sequence we are sorting is {2,1,3,4,5,6,7,8,9}, that is, except that the first and second keywords need to be swapped out, everything else is in the normal order. When i = 0 o'clock, switching between 2 and 1, the sequence is already ordered, but the algorithm still does not have to scratch the i = 2 to 9 and the J loop in each loop is executed once, although there is no exchange of data, but after a large number of comparisons are greatly redundant.
When i = 2 o'clock, we have compared the 9 with 8,8 and 7,,3 with 2, and there is no data exchange, which means that the sequence is orderly and does not need to continue the subsequent judgment work (the subsequent work will not occur any data exchange, and then do not have any meaning). To implement this idea, we need to improve the code by adding a flag variable flag to implement this algorithm:
Interchange method function swap (array & $arr, $a, $b) { $temp = $arr [$a]; $arr [$a] = $arr [$b]; $arr [$b] = $temp;} /Bubble Sort Optimization (if there is no interchange of elements at a time of the loop, the entire array is already ordered) function BubbleSort1 (array & $arr) { $length = count ($arr); $flag = TRUE; for ($i = 0; ($i < $length-1) && $flag; $i + +) { $flag = FALSE; for ($j = $length-2; $j >= $i; $j-) { if ($arr [$j] > $arr [$j + 1]) { swap ($arr, $j, $j + 1); $flag = TRUE;}}}
The key to code churn is that in the for loop of the I variable, there is an increase in the judgment of whether flag is true, and with this improvement, the bubbling sort has some performance improvements that can be avoided by the meaningless loop judgment that has been ordered.
The total time complexity of the bubbling algorithm is O (n^2).
Bubble sort is a stable sort.
This article refers to from the "Big Talk Data Structure", in this only record, convenient to consult later, great God do not spray!