Complexity analysis (bottom): Analysis of the best, worst, average, averaging time complexity

Source: Internet
Author: User

What is the analysis of time complexity?
    • Best Case time complexity (complexity)
    • Worst-case time complexity (worst complexity)
    • Average time complexity (average case complexity)
    • Averaging time complexity (amortized times complexity)
Best, worst-case time complexity

The best case time complexity is the time complexity of executing this code in the most desirable circumstances.

The best case time complexity is the time complexity of executing this code in the worst case scenario.

Take a look at the following code:

1//n Indicates the length of an array of arrays2intFindint[] Array,intNintx) {3inti = 0;4intpos =-1;5 for(; i < n; + +)i) {6if(Array[i] = =x) {7 pos =i;8 Break;9     }10   }11returnPos;12}

The function of the above code is to find the position where the variable x appears in an array and, if found, jump out of the loop, return its position value, and return 1 if it is not found.

You can't just see the for loop to determine its time complexity is O (n), because the order of the array is indeterminate, it is possible that the first element in the array is x, then you can end the loop immediately, and its time complexity is O (1); If the variable x does not exist in the array Or the last element in the array is x, it needs to traverse the entire array, and the time complexity is O (n).

Here, O (1) is the best case time complexity, and O (n) is the worst-case time complexity.

Average time complexity of the situation

The best, worst-case time complexity is the complexity of the code in extreme cases, with little probability of happening. Therefore, we also need to know the average time complexity of the situation.

As an example of the position where the variable x was just looked for, for example, the position of the variable x in the array is a total of n+1: in the 0~n-1 position of the array and not in the array. We can get an average of the number of elements to traverse in each case by looking up the number of elements that need to be traversed and dividing it by n+1:

1 + 2 + 3 + ... + n + n/n + 1 = n (n+1) +2N/2 (n+1) = N (n+3)/2 (n+1)

After simplifying the above formula, the average time complexity is O (n) after omitting the coefficient, low order and constant.

However, the n+1 of the above situation, the probability of occurrence is not the same. It is possible to introduce the relevant knowledge of probability theory, assuming that the probability of the variable x in the array and not in the array is each 1/2, the probability of appearing in the n position of 0~n-1 is 1/n. According to the law of probability multiplication, the probability that the data to be found appears anywhere in the 0~n-1 is 1/(2n).

In this way, the following calculation process can be obtained:

1 x 1/2n + 2 x 1/2n + 3 x 1/2n + ... + n x 1/2n + n x 1/2n = n (n+1) +2N/2 x 1/2n = 3N+1/4

This value is the weighted average in probability theory, also called the expectation.

Based on this weighted average, the coefficients and constants are removed, and the average time complexity we get is also O (n).

Therefore, the average time complexity is: Weighted average time complexity (also known as the expected time complexity).

In most cases, we do not need to distinguish between the best, worst, and average three complexities, and the average complexity is only used in certain special cases.

Averaging time complexity

Averaging time complexity: in a continuous set of operations for a data structure, the time complexity is very low in most cases, with only a high degree of complexity in the case of individual cases. And there's a coherent timing relationship between these operations, and at this point we can put this set of operations together to see if we can take the time of the high-complexity operation and split it into other operations that are less complex in time. (In cases where the averaging time complexity analysis can be applied, the average averaging time complexity equals the best time complexity)

The application scenario of averaging time complexity is more special and limited than the average time complexity.

As an example:

1//An array that represents a length of n2//the Array.Length in the code is equal to n3int[] Array =New int[n];4intCount = 0;5 6voidInsertintval) {7if(Count = =array.length) {8intsum = 0;9 for(inti = 0; i < Array.Length; ++i) {sum = sum +Array[i];11      }Array[0] =sum;Count = 1;14   }Array[count] =Val;17 + +count;18}

This code implements the ability to insert data into an array, and when the array is full, that is, count = = Array.Length, we use the For loop to iterate through the array summation, and then insert the new data into the time complexity O (n). However, if the array is not full, the data is inserted directly into an array with a time complexity of O (1).

Let's analyze its time complexity, the length of the array is n, according to the data inserted in different locations, can be divided into N cases, the time complexity of each case is O (1). One of the most "bad" cases is that the array is full and the time complexity is O (n). Moreover, the probability of the occurrence of this n+1 is the same, it is 1/(n+1). So, according to the calculation method of weighted average, we know:

1 x 1/n+1 + 1 x 1/n+1 + ... + 1 x 1/n+1 + n x 1/n+1 = 1

The average time complexity we've obtained is: O (1).

Comparing the example of Insert () with the example of previous find (), the best and worst-case time complexity of the two examples is the same, why is there so much difference in average time complexity?

The biggest difference between the two examples is that the best and worst-case time complexity of find () occurs in extreme cases, while insert () in most cases, the time complexity is O (1), only in extreme cases, the time complexity is O (n). Second, for the Insert () function, whenever a time complexity of O (n) is encountered, then there will be an n-1 O (1) Insert operation, which repeats itself.

The averaging time complexity of this group of successive operations is O (1). This is the general idea of averaging time complexity analysis.

Summary of Content

The best and worst-case time complexity analysis is relatively simple, but the analysis of average and averaging time complexity is comparatively complex.

These four types of complexity analysis are possible because complex levels of measurement may be different in the case of various inputs.

Complexity analysis (bottom): Analysis of Best, worst, average, averaging time complexity

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.