Big theta notation of insertion sort algorithm

I'm studying asymptotic notations from the book and I can't understand what the author means. I know that if f(n) = Θ(n^2) then f(n) = O(n^2) . However, I understand from the author's words that for insertion sort function algorithm f(n) = Θ(n) and f(n)=O(n^2) .

Why? Does the big omega or big theta change with different inputs?

He says that:

"The Θ(n^2) bound on the worst-case running time of insertion sort, however, does not imply a Θ(n^2) bound on the running time of insertion sort on every input. "

However it is different for big-oh notation. What does he mean? What is the difference between them?

I'm so confused. I'm copy pasting it below:

Since O-notation describes an upper bound, when we use it to bound the worst-case running time of an algorithm, we have a bound on the running time of the algorithm on every input. Thus, the O(n^2) bound on worst-case running time of insertion sort also applies to its running time on every input. The Θ(n^2) bound on the worst-case running time of insertion sort, however, does not imply a Θ(n^2) bound on the running time of insertion sort on every input. For example, when the input is already sorted, insertion sort runs in Θ(n) time.


Does the big omega or big theta change with different inputs?

Yes. To give a simpler example, consider linear search in an array from left to right. In the worst and average case, this algorithm takes f(n) = a × n/2 + b expected steps for some constants a and b. But when the left element is guaranteed to always hold the key you're looking for, it always takes a + b steps.

Since Θ denotes a strict bound, and Θ(n) != Θ(n²), it follows that the Θ for the two kinds of input is different.

EDIT : as for Θ and big-O being different on the same input, yes, that's perfectly possible. Consider the following (admittedly trivial) example.

When we set n to 5, then n = 5 and n < 6 are both true. But when we set n to 1, then n = 5 is false while n < 6 is still true.

Similarly, big-O is just an upper bound, just like < on numbers, while Θ is a strict bound like =.

(Actually, Θ is more like a < n < b for constants a and b, ie it defines something analogous to a range of numbers, but the principle is the same.)


Refer to CLRS edition 3 Page -44(Asymptotic notation,functions,and running times) It says -

Even when we use asymptotic notation to apply to the running time of an algorithm, we need to understand which running time we mean . Sometimes we are interested in the worst-case running time. Often, however, we wish to characterize the running time no matter what the input. In other words, we often wish to make a blanket statement that covers all inputs, not just the worst case.

Takings from the above para:

  • Worst case provides the atmost limit for the running time of an algorithm. Thus, the O(n^2) bound on worst-case running time of insertion sort also applies to its running time on every input.

  • But Theta(n^2) bound on the worst-case running time of insertion sort, however, does not imply Theta(n^2) bound on the running time of insertion sort on every input. Because best case running time of insertion sort yields Theta(n) .(When the list is already sorted)

  • We usually write the worst case time complexity of an algorithm but when best case and average case come into accountability the time complexity varies according to these cases.


  • we have a bound on the running time of the algorithm on every input

    It means that if there is a set of inputs with running time n^2 while other have less, then the algorithm is O(n^2) .

    The Θ(n^2) bound on the worst-case running time of insertion sort, however, does not imply a Θ(n^2) bound on the running time of insertion sort on every input

    He is saying that the converse is not true. If an algorithm is O(n^2) , it doesnt not mean every single input will run with quadratic time.

    链接地址: http://www.djcxy.com/p/40124.html

    上一篇: 何时使用大O符号以及何时使用大的Theta符号

    下一篇: 插入排序算法的Big Theta符号