>Can you describe an algorithm that is O(nm)?
>I'm guessing it would be something like traversing a two-dimensional array.
Good guess. That's an excellent way of describing O(nm).
>The two loops needed to do that is throwing me off however.
>It's making me think that it could be O(n^2).
If it helps, you can think of O(n^2) as O(nn), redundant as it may be. The difference here is that with O(nm), n and m don't have to be the same value. They can be, and in that case the time complexity is indeed quadratic.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Gee, why don't you figure this out for yourself?
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 176
4+ months old thread from the previous reply post (above mine)...
By the way, non-sorted 2D array with nxm will have O(nxm) because you have no uniform rule to search, so you would try to look through the whole array. Average case to me is still O(nxm)...
For a sorted 2D array, it will easily become O(logn) regardless the m and n values. Or the exact time would be log2.
Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239