1 <? PHP 2 Class Sort { 3 /* * 4 * Simple exchange sorting 5 * Basic version of Bubble Sorting 6 * This is not a standard bubble sort.AlgorithmBecause it does not satisfy the Bubble Sorting Thought of "comparing adjacent records in two or two pairs", she should be the simplest exchange sorting. 7 * Idea: Compare each keyword with the "every" keyword next to her. If it is large, it is exchanged. 8 * Disadvantage: Low Efficiency 9 */ 10 Public Function Bubblesort1 (& $ Arr ){ 11 $ Len = Count ( $ Arr ); 12 For ( $ I = 0; $ I < $ Len ; $ I ++ ){ 13 For ( $ J = $ I + 1; $ J < $ Len ; $ J ++ ){ 14 If ( $ Arr [ $ I ]> $ Arr [ $ J ]) { // /Compare each keyword with the "every" keyword following her. 15 $ This -> Swap (& $ Arr [ $ I ], & $ Arr [ $ J ]); 16 } 17 } 18 } 19 } 20 /* * 21 * Authentic Bubble Sorting Algorithm 22 * Idea: "compare adjacent records in pairs" to route the minimum value to the top 23 */ 24 Public Function Bubblesort2 (& $ Arr ){ 25 $ Len = Count ( $ Arr ); 26 For ( $ I = 0; $ I <$ Len ; $ I ++ ){ 27 For ( $ J = $ Len -1; $ J > $ I ; $ J --){ // $ J is a forward loop from the back 28 If ( $ Arr [$ J ] < $ Arr [ $ J -1]) { // Note: here is "comparing adjacent records in pairs", different from bubblesort1 29 $ This -> Swap (& $ Arr [ $ J ], & $ Arr [ $ J -1]); // Here we use the "Reference" Operator 30 } 31 } 32 } 33 } 34 /* * 35 * Improvement of the Bubble Sorting Algorithm 36 * If the array to be sorted is [2, 1, 3, 4, 5, 6, 7, 8, 9], you only need to compare and Exchange 1 and 2, the second for loop in the subsequent loop does not need to be executed, but if bubblesort2 is used 37 * The for loop after $ I = 2 to 9 will be executed again, which is obviously redundant. 38 * Improved thinking: In the for loop of the I variable, the flag is determined to be true. 39 */ 40 Public Function Bubblesort3 (& $ Arr ){ 41 $ Len = Count ( $ Arr ); 42 $ Flag = True ; 43 For ( $ I = 0; $ I < $ Len && $ Flag ; $ I ++ ){ // If no data is exchanged in the previous cycle judgment, it indicates that the current data is already in order and thus jumps out of the loop. 44 $ Flag =False ; 45 For ( $ J = $ Len -1; $ J > $ I ; $ J --){ // $ J is a forward loop from the back 46 If ( $ Arr [ $ J ] <$ Arr [ $ J -1]) { // Note: here is "comparing adjacent records in pairs", different from bubblesort1 47 $ This -> Swap (& $ Arr [ $ J ], & $ Arr [ $ J -1]); // Here we use the "Reference" Operator 48 $ Flag =True ; // If data exchange exists, set $ flag to true. 49 } 50 } 51 } 52 } 53 /* * 54 * Change the positions of $ A and $ B. 55 */ 56 Public Function Swap ( $ , $ B ){ 57 $ Temp = $ ; 58 $ = $ B ; 59 $ B = $ Temp ; 60 } 61 } 62 $ Arr = Array (, 5 ); 63 $ Test = New Sort (); 64 // $ Test-> bubblesort1 ($ ARR); // simple exchange sorting 65 // $ Test-> bubblesort2 ($ ARR); // bubble sort 66 $ Test -> Bubblesort3 ( $ Arr ); // Improved Bubble Sorting 67 ?>
Analyze its time complexity.
In the best case, that is, the table to be sorted itself is ordered, so we compare the number of times based on the last improvementCode, It can be inferred that it is a comparison of n-1, there is no data exchange, the time complexity is O (n ).
in the worst case, that is, when the table to be sorted is in reverse order, it needs to be compared and moved to an order of magnitude record. Therefore, the total time complexity is O (n ^ 2 ).