This article mainly introduces three methods for fast sorting in php, which have their own advantages and disadvantages. For more information, see
This article mainly introduces three methods for fast sorting in php, which have their own advantages and disadvantages. For more information, see
I wrote three php quick sorting examples. The first is low efficiency, but the simplest and easiest to understand. The second is the one-way Traversal method provided in the introduction to algorithms, the third type is the bidirectional Traversal Algorithm for Finding the mean value. The three algorithms are implemented and compared as follows:
Method 1: This method is intuitive, but it costs a lot of space and uses the merge function with lower efficiency. The efficiency is the lowest among the three methods. In the worst case, the algorithm degrades to (O (n * n ))
The Code is as follows:
Function quick_sort ($ array ){
If (count ($ array) <= 1) return $ array;
$ Key = $ array [0];
$ RightArray = array ();
$ LeftArray = array ();
For ($ I = 1; $ I <count ($ array); $ I ++ ){
If ($ array [$ I] >=$ key ){
$ RightArray [] = $ array [$ I];
} Else {
$ LeftArray [] = $ array [$ I];
}
}
$ LeftArray = quick_sort ($ leftArray );
$ RightArray = quick_sort ($ rightArray );
Return array_merge ($ leftArray, array ($ key), $ rightArray );
}
Method 2: This algorithm is derived from an introduction to algorithms. It is called the Nico Lomuto method (which is described in detail on goole). It uses the most classic single-direction traversal to find the median.
However, in the worst case (for example, an array with the same value requires n-1 division, and each Division requires O (n) Time to remove an element) the worst case is O (n * n)
The Code is as follows:
Function quick_sort (& $ array, $ start, $ end ){
If ($ start >=$ end) return;
$ Mid = $ start;
For ($ I = $ start + 1; $ I <= $ end; $ I ++ ){
If ($ array [$ I] <$ array [$ mid]) {
$ Mid ++;
$ Tmp = $ array [$ I];
$ Array [$ I] = $ array [$ mid];
$ Array [$ mid] = $ tmp;
}
}
$ Tmp = $ array [$ start];
$ Array [$ start] = $ array [$ mid];
$ Array [$ mid] = $ tmp;
Quick_sort ($ array, $ start, $ mid-1 );
Quick_sort ($ array, $ mid + 1, $ end );
}
Method 3: This method is basically a common textbook method. First, we traverse from left to right less than the middle element, and traverse from right to left to skip a large element.
If the values of the two sides are not crossed, continue the loop until the intermediate point is found. Note that this method is still exchanged when processing the same element, so there is also O (nlogn) in the worst case)
Efficiency. But in the following function, if you change $ array [$ right]> $ key to $ array [$ right]> = $ key or change $ array [$ left] <$ key to $ array [$ left], <= $ key is the worst
This will not only degrade to O (n * n), but also produce additional overhead for n interactions in addition to the consumption of each comparison. There are two other test sites for students who are hard to remember:
1: The two while in the middle can be exchanged. Of course, it cannot be exchanged, because an extra space is required for the fast disk to save the initial left value. In this way, when the left and right sides are swapped, they are overwritten with the right and saved.
Is the left value of the value. Otherwise, an error occurs. See this sentence $ array [$ left] = $ array [$ right];
2: $ array [$ right] = $ key; can this statement be omitted. This sentence cannot be omitted. You can consider an extreme situation, such as sorting two values (5, 2), and gradually understand it.
The Code is as follows:
Function quick_sort_swap (& $ array, $ start, $ end ){
If ($ end <= $ start) return;
$ Key = $ array [$ start];
$ Left = $ start;
$ Right = $ end;
While ($ left <$ right ){
While ($ left <$ right & $ array [$ right]> $ key)
$ Right --;
$ Array [$ left] = $ array [$ right];
While ($ left <$ right & $ array [$ left] <$ key)
$ Left ++;
$ Array [$ right] = $ array [$ left];
}
$ Array [$ right] = $ key;
Quick_sort_swap (& $ array, $ start, $ right-1 );
Quick_sort_swap (& $ array, $ right + 1, $ end );
}