- 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 ). Code:
- 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-style method. First, we traverse from left to right less than the middle element, and traverse from right to left to skip a large element, then, if the values of the two sides are not crossed, continue the loop until the intermediate point is found. Note that this method still exchanges the same elements, so that the O (nlogn) efficiency is also possible in the worst case. But in the following function, if you change $ array [$ right]> $ key to $ array [$ right]> = $ key or change $ array [$ left] <$ key to $ array [$ left], <= $ key, the worst case is not only O (n * n ). in addition to the consumption of each comparison, it also produces additional overhead of n interactions. There are two other test points for this question: 1. Can the two while in the middle 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 exchanged, the left value that has been saved as the value is overwritten on the right, otherwise, problems may occur. See $ 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. Code:
- 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 );
- }
Articles you may be interested in:
- Php instance code of the practical quick sort algorithm
- Php instances associated with array sorting and quick sorting
- Php quick sort function
- Php functions for fast sorting
- Example of php bubble sorting and quick sorting
|