简体   繁体   English

g(n)复杂度为O(n ^ 2)的算法的时间复杂度

[英]Time complexity of algorithm with g(n) complexity of O(n^2)

I have just learnt about the time complexity of various sorting method. 我刚刚了解了各种排序方法的时间复杂性。 (eg Merge Sort, quick sort) However i am still a beginner at this field. (例如合并排序,快速排序)但是,我仍然是该领域的初学者。 I know that if g(n) has a complexity of O(n) the whole time complexity of this method would be n logn. 我知道如果g(n)的复杂度为O(n),则此方法的整个时间复杂度将为n logn。 But what if the complexity of g(n) is O(n^2)? 但是,如果g(n) is O(n^2)?的复杂度g(n) is O(n^2)?

void f(n) { 
    if (n <= 1) return;
    else { 
      g(n); 
      f(n/2);
      f(n/2);
    }
}

The time complexity recurrence relation is 时间复杂度递归关系为

在此处输入图片说明

– where G(n) is the time complexity of g(n) . –其中G(n)g(n)的时间复杂度。 Methods to solve this for eg O(n^2) : 解决此问题的方法,例如O(n^2)

  1. Expansion (dropping the O(...) until the end): 扩展(将O(...)放到最后):

    在此处输入图片说明

    – after m expansions. m扩展后。 The second term contains a geometric series which converges to 2 , so it can be ignored as a constant. 第二项包含一个收敛为2几何级数 ,因此可以将其视为常量忽略。 Applying the stopping condition n = 1 : 应用停止条件n = 1

    在此处输入图片说明

  2. The Master Theorem . 大师定理 For the example: 例如:

    在此处输入图片说明

    – which means Case 3 applies. –这意味着情况3适用。 Therefore the result is consistent with (1): 因此,结果与(1)一致:

    在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM