Data Structures and Algorithms: An Indicator for Measuring the Quality of an Algorithm—Complexity

1. Complexity

Complexity is used to analyze the resources required during the execution of the algorithm.
Time complexity is measured by the time required.
Space complexity is a measure of the (memory) space required.

1.1 Time complexity

characteristic

1. Measure the time required for algorithm execution
2. Estimated based on the number of "constant operations"
3. Generally measured by the maximum amount of data N Benchmark

How to express?

Represented by O(x) notation

O is used to represent the worst case; θ represents the average case; Ω represents the best case

Time complexity actually shows a kind oftrend. As the amount of data increases, how does the time consumed increase ( The changing trend of the number of constant operations)

Common complexity

O(n²)

represents exponential growth

O(n)

linear growth

O(logn)

Logarithmic growth

O(1)

No growth

Use the function image to represent the time complexity:
Insert image description here

When the amount of data is extremely small, the advantages and disadvantages of the algorithm may not be obvious. Only when the amount of data is extremely large, it is meaningful to discuss the time complexity.

1.2 Space complexity

Similarly, space complexity refers to the growth trend of the required memory size. For example, if it is used to store N numbers, the space complexity is O(N), if it is used to store a limited number, the space complexity is O(1)... and so on.

The numbers or letters in O() represent a trend rather than a specific value.

extra space complexity

In addition to the known occupied space, how much space is required to complete the algorithm.

Guess you like

Origin blog.csdn.net/yztbydh/article/details/134985355
Recommended