A quick sort of sorting algorithm

Source: Internet
Author: User

The idea of quick sequencing:

Divide and conquer, divide the big problem into several small problems, solve the small problem and synthesize the solution of big problem

Typical quick ordering of the general process:

 1, in the array to find a number, the general election count group last number as the number of axes X

2, with the axis number x as the center, using a division partition, so that the number of the left of the axis number is smaller than x, the number on the right is larger than X, in other words, after a division, the axis number x in the position where it should be

3, recursive call partition, and finally make the whole array orderly

Once dividing the partition process:
1 intPartitionintData[],intStartintend) {2     inti=start-1; 3     intj=start;4     intx=Data[end];5      for(; j<end;j++){6         if(data[j]<x) {7++i;8Exchange (data+i,data+j);9         }Ten     } OneExchange (data+i+1, data+end); A     returni+1;//I identifies the largest subscript in the array with a value less than x, and i+1 refers to the position of the subscript where x is positioned once divided -}
Procedure for recursive invocation:
 1  void  quicksort (int  data[],int  start,int   end) { 2  if  (Start<end) { 3  int  index=partition (data,start,end);  4  quicksort (data,start,index-1  ); 5  quicksort (data,index+1  ,end);  6   7  }

Note: The boundary check condition of the function can be delivered to the upper calling function to complete

Time complexity analysis for fast sequencing:

  For the sake of simple explanation, always select the last element of the array to be sorted as the number of the middle axis, assuming an extreme case, the array is ordered or the element values are all the same, after a partition division, the number of elements on the left and right sides of the axis is n-1,0, that is, each after a division, Just place an array element where it should be, the remaining number of N-1 needs to be sorted, and the time complexity of each partition is the time it takes to traverse an array to be queued O (n)

Set the time complexity for the whole sort to T (N), then the worst case, T (n) =t (n-1) +t (0) +o (n), can be recursive t (n) =o (n^2)

The optimal time complexity of the algorithm, in the most ideal case, each partition will always divide the array to be divided, that is, after the completion of a partition, the number of elements on both sides of the middle axis is (N-1)/2, then its total time complexity

T (n) <=2*t (N/2) +o (n), t (n) =o (NLGN)

The average time complexity of the algorithm, T (n) =o (NLGN), (any division of a constant scale produces a recursive tree with a depth of O (LGN), and the time complexity of each layer is O (n) for a time division, so the total time complexity is O (NLGN)---- Algorithm Introduction , it can be understood that if a layer of the division effect is poor, then the division below the layer may be better, good division of random distribution in the recursive tree of the various layers, the total division gradually in each layer is a good division of the situation.

Stability for fast sequencing:

  It is known from the three processes of the quick row that the fast line is not a stable sort , for example [2,5,6,4 (1), 7,3,8,4 (2)], after a division, the array becomes [2,3,4 (2), 4 (1), 7,5,8,6], and the brackets identify the 1th and 2nd 4.

Quick-Sort improvements:

  Quick sorting is required for the randomness of the input array, and if the array element to be sorted has a greater randomness, the sort effect is close to the lower bound O (NLGN) of the comparison-based sorting method, which is not suitable for quick sorting if the input array is basically ordered.

Therefore, for the array to be sorted, you can give a random version of the fast row, each time before partitioning, randomly select a number T in the array and the last number of X Exchange location, after the exchange call the above partition function, so that the random selection of the element T as the number of axes, so as to optimize the performance of the sorting.

Reference: Introduction to Algorithms

A quick sort of sorting algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.