I'd like to think this is true, but I'm not too confident in that answer. Is there an algorithm that has an equal running time in the both the average and worst case. I'm not sure if the answer would be true then.
Asked
Active
Viewed 111 times
0
-
If the `avg` and `worst` case times where equal, then the `best` case time would have to be equal to them both either, making your algorithm basically constant time. Otherwise you'd have an `avg` between the `best/worst` boundaries. – Regexident Apr 04 '13 at 23:21
-
The average case is never worse than the worst case. It can be the same or better. – Alexey Frunze Apr 04 '13 at 23:26
-
@wildplasser I feel that if it is false even once then the overall answer would be false. – blutuu Apr 04 '13 at 23:33
2 Answers
4
Calculating 1+1=2 is O(1) in best, average, and worst case.
A slightly less trivial example: determining the length of a linked list of length n is takes n steps in all cases, so it's O(n) in all cases.

Joni
- 108,737
- 14
- 143
- 193
-
...unless you maintain the length separately as you add and remove items – angelatlarge Apr 04 '13 at 23:22
-
In this case the average technically isn't better. Would that render the question false? – blutuu Apr 04 '13 at 23:35
-
Yes. If the average case is equal to the worst case, the average case is not better than the worst case. – Joni Apr 04 '13 at 23:40