3 ms·
You'll need to be careful what your N is and what you're counting. But lets assume the input is an array of n characters, n being an arbitrarily large but fixe
by tetha 6y ago
You'll need to be careful what your N is and what you're counting.
But lets assume the input is an array of n characters, n being an arbitrarily large but fixed positive number during a single invocation. And we're estimating the number of char compares.
Then, each invocation of isVowel(c) will do 1 - 5 compares. 1 in the case that c is the first element of the array, 5 if c is the last element or no element. As a worst-case estimation, isVowel(c) will do 5 compares at worst.
As such, if isVowel(c) is called for all elements of A, we will see between n compares (best case for all invocations) and 5*n compares (worst case for all compares).
As such, your algorithm has a worst-case runtime of O(5n) for all arrays A, which is a subset of O(n).
In practice and outside of academia, that's pretty much all you do:
- Identify your input set, and estimate its size
- Count how many loops are nested in the algorithm, call that L (1 in this case)
- Your algorithm should be O(n^L).
Once it gets into amortization and such... it becomes much harder.