Naruto Uzumaki
Naruto Uzumaki

Reputation: 91

Best case of an algorithm

If I have a recursive algorithm for a certain data structure for example

Algo(Tree T)
    if T == null
        return false
    ...

Will the best case of this algorithm be when the tree is null? The time complexity is O(1)?

Upvotes: 0

Views: 282

Answers (2)

Kevin Wang
Kevin Wang

Reputation: 2729

Usually when talking about best-case time complexity for an algorithm, we would say it's the best-case for an input of size n.

For example, consider a sorting algorithm. If the sorting algorithm makes no changes and only iterates once if the input list is already sorted, then we'd say that in the best case, the algorithm is O(n). Of course, the input list could be empty, but that has no bearing on the way the number of operations grows as n increases.

Upvotes: 3

MBo
MBo

Reputation: 80327

No, you have to consider work with structure of size N > 0

For empty data almost algorithms would have O(1) complexity (except for ones that prepare internal data before input checking)

Upvotes: 0

Related Questions