as far as I got it linear complexity can be represented as simple loop and quadratic complexity can be represented as nested loop. How can the cubic and logarithmic complexity be represented?
Thanks!
A simple loop can have logarithmic complexity, e.g.
for (i = 1; i <= N; i *= 2)
...
As others have already answered, a triple nested loop will have cubic complexity.
Since cubic is O(n^3), it would be three nested loops.
Logarithmic is not so straightforward and usually requires a recursive relation. For example, MergeSort is O(n*log(n)) because it forms a recursion tree of height log(n) and each level requires an O(n) merging operation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With