Example of algorithm which has different worst case upper bound, worst case lower bound and best case bounds?

放肆的年华 提交于 2019-12-04 19:19:39

Here's a less natural but perhaps more satisfying definition of H. This function computes the cube of the sum of the input list in a rather silly manner.

def H(lst):
    s1 = 0
    for x in lst:
        s1 += x
    if s1 == 0:
        return 0
    elif len(lst) % 2 == 0:
        s2 = 0
        for x in lst:
            for y in lst:
                s2 += x * y
        return s1 * s2
    else:
        s3 = 0
        for x in lst:
            for y in lst:
                for z in lst:
                    s3 += x * y * z
        return s3

The best-case bound is Theta(n). The tightest worst-case upper bound of the form O(n^k) is O(n^3). The tightest worst-case lower bound of the form Omega(n^k) is Omega(n^2).

Note, however, that the tightest bound of any form on the worst-case is Theta(f(n)) where f(2m) = m^2 and f(2m+1) = m^3.

The way you describe it, pick any quadratic sort algorithm, say bubble sort:

  • Worst case upper bound can be said to be O(n^3).

  • Worst case lower bound can be said to be Ω(n^2).

  • The best case running time can be said to be Θ(n), when the input is already sorted and you check this with a single pass before starting the algorithm, or use optimizations that terminate the algorithm prematurely in this case.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!