Reputation: 11
What is the relation/difference between worst case time complexity of an algorithm and its upper bound?
Upvotes: 1
Views: 842
Reputation: 1
The difference between worst case and big O(UPPER BOUND) is that the worst case is a case that actually happens to your code, the upper bound is an overestimate, an assumption that we put in order to calculate the big O, it doesn't have to happen
example on insertion sort:
Worst Case:
The numbers are all arranged reversely so you need to arrange and move every single number
Pseudo-code
for j=2 to n
do key = a[i]
i=j-1
while i>0 & a[i]>key
do a[i+1] = a[i]
i=i-1
end while
a[i+1]=key
end for
Upper Bound:
We assume that the order of the inner loop is i =n-1 every single time, but in fact, it is changeable every time, it can't be n-1 every time, but we assumed /overestimated it to calculate the big O
Upvotes: 0
Reputation: 178451
The term "upper bound" is not very clear, as it may refer to two possible things:
The upper bound of the algorithm - the bound where the algorithm can never run "slower" than it. This is basically the worst case performance of it, so if this is what you mean - the answer is pretty simple.
big-O notation, which provides an upper bound on the complexity of the algorithm under a specific analysis. The big-O notation is a set of functions, and can be applied to any analysis of an algorithm, including worst case, average case, and even best case.
Let's take Quick Sort as an example.
Quick Sort is said to have O(n^2)
worst case performance, and O(nlogn)
average case performance. How can one algorithm has two complexities? Simple, the function representing the analysis of average case, and the one representing the worst case, are completely different funcitons - and we can apply big O notation on each of them, there is no restriction about it.
Moreover, we can even apply it to best-case. Consider a small optimization to quick-sort, where it first checks if the array is already sorted, and if it is - it stops immidiately. This is effectively O(n)
operation, and there is some input that will provide this behavior - so we can now say that the algorithm's best case complexity is O(n)
Upvotes: 3