/** * Bubble sort: stable, time complexity O (n^2) * Bubble Sort method is the simplest sort method. The basic idea of this approach is to think of the elements to be sorted as "bubbles" that are vertically arranged, smaller elements lighter, and thus upward. In the bubble sorting algorithm we have to deal with this "bubble" sequence several times. * The so-called one-time processing, is to check the sequence from the bottom up, and always note the sequence of two adjacent elements is correct. * If two adjacent elements are found to be in the wrong order, then the "light" elements are below, exchanging their positions. * Obviously, after processing, the "lightest" element floats to the highest position, and after two times, the "light" element floats to the next high position. * In the second pass, you do not have to check because the element at the highest position is already the lightest element. * Generally, when I pass the processing, I do not have to check the high position above the elements, because the previous i-1 through the processing, they have been properly sequenced. * @param Array $arr * @return Array */ functionbubblesort(array $arr){$count= Count ($arr);if($count>1){//means to run N times for($i=0;$i<$count;$i++){//Indicates the number of times to compare each trip//After comparison, the largest number is on the far right and can be compared less once, so subtract $ for($j=$i+1;$j<$count-$i-1;$j++){if($arr[$j] >$arr[$j+1]){$temp=$arr[$j];$arr[$j] =$arr[$j+1];$arr[$j+1] =$temp; } } } }Else{return$arr; }}
'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
'). Text (i)); }; $numbering. FadeIn (1700); }); });
The above describes the bubble sort of PHP implementation, including the bubble sort, PHP content, I hope the PHP tutorial interested in a friend helpful.