Data structure and algorithm of time complexity Detailed

Data structure and algorithm of time complexity Detailed


table of Contents

  1. Introduction and classification sorting algorithm
  2. The time complexity of the algorithm concept
  3. Common time complexity resolved
  4. The average time complexity of complexity and worst time
  5. Space complexity introduced

1. Introduction sorting and classification algorithms

  1. Describes a sorting algorithm
    to sort also known sorting algorithm (SortAlgorithm), the sorting process is a set of data, arranged according to the order specified.

  2. Sort of classification:

    1. Internal sorting:
      refers to all the data to be processed are loaded into the internal memory are sorted (memory).
    2. External sorting method:
      data is too large to load into memory all need to sort the help of external memory (files).
    3. Common sorting algorithm classification (see below):Here Insert Picture Description

2. The concept of time complexity of the algorithm

Both methods (algorithms) 1. Measuring the execution time of a program
1. 事后统计的方法

This method is feasible, but there are two problems: First, in order to run performance of the algorithm design for evaluation, you need to actually run the program; the second is the amount of income statistics time depends on environmental factors computers hardware, software, etc., which ways to run in the same state on the same computer, in order to compare the algorithm faster.

2. 事前估算的方法

Through the analysis of an algorithm 时间复杂度to determine which algorithm is better.


2. Time and Frequency
  1. Basic introduction
    time frequency: time spent and the number of times an algorithm execution algorithm is proportional to the statement, the statement is executed which algorithm more often, it takes time and more. A number of execution algorithms statement called the statement frequency or time frequency. Referred to as T (n).
  2. Examples
    such as 1-100 sum of all the numbers, we designed two algorithms calculate:

Here Insert Picture Description

  1. Time frequency ignore the constant term
    Here Insert Picture Description
    conclusions:
  1. 2n and 2n + 20 with n becomes large, infinitely close to perform curve, 20 can be ignored
  2. 10 + 3n and 3n as n becomes large, infinitely close to perform curve, 10 can be ignored
  1. Time and frequency of the low-order terms ignored
    Here Insert Picture Description
    conclusion:
  1. 2n ^ 2 + 3n + 10 with n and 2n ^ 2 becomes large, infinitely close to the curve performed, can be ignored 3n + 10
  2. n^2+5n+20 和 n^2 随着n 变大,执行曲线无限接近, 可以忽略 5n+20
  1. 时间频率之忽略系数
    Here Insert Picture Description

结论:

  1. 随着n值变大,5n^2+7n 和 3n^2 + 2n ,执行曲线重合, 说明 这种情况下, 5和3可以忽略。
  2. 而n^3+5n 和 6n^3+4n ,执行曲线分离,说明多少次方式关键
3. 时间复杂度
  1. 一般情况下,算法中的基本操作语句的重复执行次数是问题规模n的某个函数,用T(n)表示,若有某个辅助函数f(n),使得当n趋近于无穷大时,T(n) / f(n) 的极限值为不等于零的常数,则称f(n)是T(n)的同数量级函数。记作 T(n)=O( f(n) ),称O( f(n) ) 为算法的渐进时间复度,简称时间复杂度。

  2. T(n) 不同,但时间复杂度可能相同。 如:T(n)=n²+7n+6 与 T(n)=3n²+2n+2 它们的T(n) 不同,但时间复杂度相同,都为O(n²)。

  3. 计算时间复杂度的方法:

    • 用常数1代替运行时间中的所有加法常数 T(n)=n²+7n+6 => T(n)=n²+7n+1
    • 修改后的运行次数函数中,只保留最高阶项 T(n)=n²+7n+1 => T(n) = n²
    • 去除最高阶项的系数 T(n) = n² => T(n) = n² => O(n²)

3. 常见的时间复杂度解析

  1. 常数阶O(1)
  2. 对数阶O(log2n)
  3. 线性阶O(n)
  4. 线性对数阶O(nlog2n)
  5. 平方阶O(n^2)
  6. 立方阶O(n^3)
  7. k次方阶O(n^k)
  8. 指数阶O(2^n)

说明:

  1. 常见的算法时间复杂度由小到大依次为:Ο(1)<Ο(log2n)<Ο(n)<Ο(nlog2n)<Ο(n2)<Ο(n3)< Ο(nk) <Ο(2n) ,随着问题规模n的不断增大,上述时间复杂度不断增大,算法的执行效率越低
  2. 从下图中可见,我们应该尽可能避免使用指数阶的算法
    Here Insert Picture Description
常见时间复杂度详解
  1. 常数阶O(1)
    无论代码执行了多少行,只要是没有循环等复杂结构,那这个代码的时间复杂度就都是O(1)
    Here Insert Picture Description
    上述代码在执行的时候,它消耗的时候并不随着某个变量的增长而增长,那么无论这类代码有多长,即使有几万几十万行,都可以用O(1)来表示它的时间复杂度。

  2. 对数阶O(log2n)
    Here Insert Picture Description
    说明:在while循环里面,每次都将 i 乘以 2,乘完之后,i 距离 n 就越来越近了。假设循环x次之后,i 就大于 2 了,此时这个循环就退出了,也就是说 2 的 x 次方等于 n,那么 x = log2n也就是说当循环 log2n 次以后,这个代码就结束了。因此这个代码的时间复杂度为:O(log2n) 。 O(log2n) 的这个2 时间上是根据代码变化的,i = i * 3 ,则是 O(log3n)

  3. 线性阶O(n)
    Here Insert Picture Description
    说明:这段代码,for循环里面的代码会执行n遍,因此它消耗的时间是随着n的变化而变化的,因此这类代码都可以用O(n)来表示它的时间复杂度

  4. 线性对数阶O(nlogN)
    Here Insert Picture Description
    说明:线性对数阶O(nlogN) 其实非常容易理解,将时间复杂度为O(logn)的代码循环N遍的话,那么它的时间复杂度就是 n * O(logN),也就是了O(nlogN)

  5. 平方阶O(n²)
    Here Insert Picture Description
    说明:平方阶O(n²) 就更容易理解了,如果把 O(n) 的代码再嵌套循环一遍,它的时间复杂度就是 O(n²),这段代码其实就是嵌套了2层n循环,它的时间复杂度就是 O(nn),即 O(n²) 如果将其中一层循环的n改成m,那它的时间复杂度就变成了 O(mn)


平均时间复杂度和最坏时间复杂度

  1. 平均时间复杂度是指所有可能的输入实例均以等概率出现的情况下,该算法的运行时间。

  2. Time complexity in the worst case, said the worst time complexity. The time complexity of general discussion are the time complexity in the worst case. The reason for this is: time complexity is the worst-case running time limit on any input instance, which ensures the running time of the algorithm is not longer than the worst case.

  3. The average time complexity and worst time complexity is consistent, and the relevant algorithm (FIG).
    Here Insert Picture Description


The space complexity introduced

  1. Similar to the discussion of time complexity, space complexity of an algorithm (Space Complexity) for defining the algorithm storage space consumed, which is also a function of the problem size n.
  2. The complexity of the space (Space Complexity) algorithm is a temporary occupation measure of the size of the storage space during operation. Some algorithms require the number of units occupied by temporary work and solve problems related to the size of n, n, as it increases, when n is large, will take up more memory cells, such as quick sort and merge sort algorithm this is the case
  3. In doing algorithm analysis, the main question is the time complexity. From the user experience point of view, more attention to the speed of program execution. Some caching products (redis, memcache) and algorithms (radix sort) is essentially a space for time.

Guess you like

Origin blog.csdn.net/weixin_41910694/article/details/93002280