Introduction to Algorithms-Quick sorting

Source: Internet
Author: User
Tags virtual environment

Description of a quick sort

The quick sort is based on the divide-and-conquer strategy. The three steps to a sub-array A[P...R] Quick sort of the divide-and-conquer process are:

1. decomposition

Array A[P...R] is divided into two (possibly empty) sub-arrays a[p...q-1] and A[Q+1...R], so that each element in a[p...q-1] is less than or equal to a[q], and is less than or equal to the elements in A[Q+1...R]. Subscript Q is also calculated during this partitioning process.

2. Solve

Sort by recursive invocation of quick Sort, sub-array a[p...q-1] and A[Q+1...R].

3. merger

Because two word groups are sorted in place, merging them does not require an action: the entire array A[P...R] is sorted.

The Quick Sort reference code is as follows:

1 classSolution2 {3  Public:4     voidQuickSort (int*a,intlength);5     voidQSort (int*a,intLowintHigh );6     intPartition (int*a,intLowintHigh );7 };8 9 voidSolution::quicksort (int*a,intlength)Ten { OneQSort (A,0, Length-1); A } -  - voidSolution::qsort (int*a,intLowintHigh ) the { -     if(Low <High ) -     { -         intp =Partition (A, low, high); +QSort (A, low, p-1); -QSort (A, p +1, high); +     } A } at  - intSolution::P artition (int*a,intLowintHigh ) - { -     intKey =A[high], tmp; -     inti = low-1, J; -  in      for(j = Low, j < high; + +)j) -     { to         if(A[j] <=key) +         { -i = i +1; the  *TMP = A[j];//Exchange A[j] and A[i] $A[J] =A[i];Panax NotoginsengA[i] =tmp; -         } the     } +  ATMP = A[high];//Exchange A[high] and A[i + 1] theA[high] = a[i +1]; +A[i +1] =tmp; -  $     returni +1; $}

Another visual notation for function partition () is as follows:

1 intSolution::P artition (int*a,intLowintHigh )2 {3     intKey =A[low];4     5      while(Low <High )6     {7          while(Low < high && key <=A[high])8         {9--High ;Ten         } OneA[low] =A[high]; A  -          while(Low < high && key >=A[low]) -         { the++Low ; -         } -A[high] =A[low]; -     } +A[low] =key; -  +     returnLow ; A}

Second, the performance of fast sorting

the run time of a fast sort is related to whether the partition is symmetrical, and the latter is related to which element is chosen. If the partitioning is symmetric, the algorithm is as fast as the merge sort in a progressive sense, and if the partitioning is asymmetric, the algorithm is as slow as the insertion algorithm in the progressive sense. the function partition () runs on the sub-array A[P...R], where n=r-p+1.

1. Worst Case Division

The worst case division occurs when the two regions generated during the partitioning process contain n-1 elements and a 0 element respectively. This asymmetric partitioning is assumed for each recursive invocation of the algorithm. The time cost of dividing is that the time cost for recursive invocation of an array of size 0 is that the run time of the algorithm can be expressed as:

, solved .

Therefore, if the algorithm in each layer recursion, the division is the maximum degree of asymmetry, the running time of the algorithm is. Thus , the worst-case run time of the fast sorting algorithm is no better than the insertion sort. For example: When the input array is fully sequenced, the run time for the quick sort is, and the insertion sort runs at.

2. Best Case Division

The function partition () can do the most balanced partitioning, resulting in a size of two sub-problems close to N/2. In this case, the quick sort runs much faster. The running time of the algorithm can be expressed as:, solved .

3, the division of the balance

The average run time for fast sorting is close to its best-case run time, not very close to its worst-case run time. For example: assuming that the partitioning process always produces 9:1 of the partition, the algorithm run time can be expressed as:. recursive tree corresponding to this recursive recursive

Each layer of the tree is at the cost of CN until it terminates at depth. The total cost of such a quick sort is.

On each level of recursion, according to the proportion of 9:1, it seems to be quite unbalanced, but from a progressive point of view, it is the same as the effect of dividing in the middle. In fact, according to the 99:1 division of the running time is also, because any one in accordance with the constant proportion of the Division will produce a depth of the recursive tree, where the cost of each layer, and therefore, whenever the constant proportion of the total run time is.

Iii. Conclusion

1. Advantages: Because each trip can determine the position of more than one element, and exponentially increase, so particularly fast. Premise: Sequential storage structure.

2, change the selection method of dividing elements, at most can only change the average time performance of the algorithm, can not change the worst-case time performance.

3, is an unstable sorting algorithm, and is not in-situ sorting.

4, for the input array containing n number, the worst run time is. Although this worst-case run time is poor, fast sorting is often the best practical choice for sorting because of its average performance and the fact that the constant factor implied in the notation is small. work well in a virtual environment.

Introduction to Algorithms-Quick sorting

Related Article

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.