Optimal time complexity, average time complexity and worst-time complexity of fast-running

Source: Internet
Author: User
Tags comparison sort

Let's analyze the performance of the fast sorting method. The time performance of fast sorting depends on the depth of the fast sort recursion, and recursive tree can be used to describe the execution of the recursive algorithm. As shown in Figure 9‐9‐7, it is the recursive process of {50,10,90,30, 70,40,80,60,20} During the quick sort process. Since our first keyword is 50, which is exactly the middle value of the sequence to be sorted, the recursive tree is balanced, and performance is better at this time.

Figure 9-9-7
In the best case, the partition is evenly spaced every time, and if you sort n a keyword, the depth of the recursive tree is. log2n.+ 1 (. x. Represents the largest integer not greater than x), that is, only recursive log2n times, the time is T (N), the first time partiation should be to scan the entire array, do n times comparison. Then, the obtained pivot divides the array into two, then the time of T (N/2) is required (note the best case, so split in half). So we continue to divide, we have the following inequality inference.
T (n) ≤2t (N/2) +n,t (1) =0 t (n) ≤2 (2T (N/4) +N/2) +n=4t (N/4) +2n T (n) ≤4 (2T (N/8) +N/4) +2n=8t (N/8) +3n ... T (n) ≤nt (1) + (log2n) xn= O (NLOGN)

In other words, in the optimal case, the time complexity of the fast sorting algorithm is O (NLOGN).

In the worst case, the sequence to be sorted is either positive or reverse, and each partition has only one sub-sequence that is less than the last one, noting that the other is empty. If the recursive tree is drawn, it is a diagonal tree. At this point the need to perform n‐1 recursive call, and the first Division I need to go through the N‐i keyword comparison to find the first record, that is, the position of the pivot, so the number of comparisons, and finally the time complexity of O (N2).

In the average case, the PIVOT keyword should be in position K (1≤k≤n), then:

It can be proved by mathematical induction that its order of magnitude is O (Nlogn).

In terms of space complexity, the main is the use of recursive stack space, the best case, the depth of the recursive tree is log2n, its spatial complexity is O (LOGN), the worst case, the need for n‐1 recursive call, its spatial complexity is O (n), the average situation, space complexity is O (logn).

Unfortunately, because the comparison and exchange of keywords is jumping, so fast sorting is an unstable sort method.

Reference: http://book.51cto.com/art/201108/287089.htm

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.