by bogomipz · 7y ago · 0 comments
This might potentially do someone a disservice. The whole point of analysis is just that - to "analyze" the runtime and not resort to "rules of thumb."
The presence of nested for loops itself is not a good reason to declare something quadratic. Consider the following which is most certainly not O(N^2):
for (int i = 0; i < 1000; i++)
for (int j = n; j >= 1; j = j / 2)
// do something
EDIT: initialized j to the val of n on the inner loop per the comments below.if for (int i = 0; i < 1000; i++) were instead
for (int i = 0; i < n; i++) - You'd have O(n)
If also for (int j = 1000; j >= 1; j = j / 2) were instead
for (int j = n; j >= 1; j = j / 2) - Then you'd have O(n log n)
OR for (int j = n; j >= 1; j -= 1) - O(n^2)
OR for (int j = n * 1000; j >= 1; j -= 1) - this is STILL O(n^2)
edit: you've now edited the inner loop to be n, but the outer loop is still 1000. This is still wrong, as the entire algorithm is now just O(lg n).
That would be a sign that you as a candidate are being needlessly pedantic and usually a red flag.