Big-O Complexity Explainer

Reads loop headers and simple recursion to estimate Big-O, explains the multiplication of nested loops, charts growth rates and times them for large n.

S'executa al teu navegador. Els fitxers mai no surten del dispositiu. Mira com comprovar-ho

…

Com es fa

  1. 1
    Paste code

    Type or paste code or pseudo-code, or press an example such as Loop with halving inside.

  2. 2
    Read the estimate

    The result box shows the order and the table lists each loop with its line, nesting and cost.

  3. 3
    Follow the reasoning

    The bullet list multiplies the costs along each chain of nested loops.

  4. 4
    Compare growth

    Open Growth chart, choose the range and switch the logarithmic axis on or off.

  5. 5
    Estimate run time

    Enter an input size n and operations per second to see the time for each order.

  6. 6
    Practise

    Practice shows a snippet; type its order, such as n log n or n^2.

Sobre aquesta eina

Big-O notation says how the running time of an algorithm grows as the input gets larger, ignoring constant factors. Most exam questions come down to counting loops: nested loops multiply, loops in sequence add, a loop whose counter doubles or halves runs about log n times, and a loop up to √n runs √n times. This explainer reads code or pseudo-code in C, Java, JavaScript or Python style, finds each loop, classifies it, follows the nesting and reports the estimated order with a line-by-line explanation. It also recognises common recursion patterns: one call on n - 1, one call on half the input (binary search), two calls on halves (merge sort) and two calls on n - 1 (naive Fibonacci). The growth tab plots the common orders on a normal or logarithmic scale and converts each into an estimated running time for an input size and machine speed you choose, which makes the difference between n log n and n² hard to forget.

How the estimate is made

The tool looks at loop headers. A for loop up to n, len(a) or a.length counts as n. A counter multiplied or divided by a constant, in the header or in the body of a while loop, counts as log n. A condition like i·i ≤ n counts as √n. A loop with a fixed bound such as i < 10 counts as constant. An inner loop that runs up to the outer counter is still order n, because 0 + 1 + … + (n - 1) = n(n - 1)/2.

The rules of thumb

Multiply along a chain of nested loops; for loops one after another keep the biggest term; drop constants and lower-order terms. So two separate loops over n are O(n), not O(2n), and n² + n log n is O(n²).

Recursion

For recursive functions the cost follows a recurrence. T(n) = T(n/2) + O(1) gives O(log n), T(n) = 2T(n/2) + O(n) gives O(n log n) by the master theorem, and T(n) = 2T(n - 1) + O(1) gives O(2^n). The tool detects these patterns from how the function calls itself.

Worked example

A loop over n containing a while loop that doubles j until it reaches n does n × log2 n steps: O(n log n). For n = 1,000,000 that is about 2 × 10^7 steps, roughly 0.2 seconds at 10^8 simple operations per second, while an O(n²) algorithm needs 10^12 steps, close to three hours.

Limits

This is a pattern-based estimate from the text of the code; it does not run your program. Loops whose bounds depend on data, early breaks, while loops with complex updates, and calls to library functions with their own costs are not analysed, so always check the reasoning. Space complexity and amortised analysis are not covered.

Exemple resolt

Outer loop over n with an inner doubling loop: n × log n = O(n log n); for n = 10^6 about 2 × 10^7 steps (0.2 s).

En resum

Preguntes freqüents

How does the tool decide the complexity?

It classifies each loop header (n, log n, √n or constant), multiplies the costs along nested loops and keeps the largest chain. Simple recursion patterns are recognised separately.

Why is a triangle loop still O(n²)?

The inner loop runs 0, 1, 2, … up to n - 1 times, which adds up to n(n - 1)/2. Dropping the constant 1/2 leaves O(n²).

Why does a doubling counter give log n?

Doubling from 1 reaches n after log2 n steps, so the loop body runs about log n times.

Does it run my code?

No. It only reads the structure of the text, so nothing is executed and any language-like pseudo-code works.

What does the growth chart show?

The number of steps for each common order as n grows, on a normal or logarithmic axis, plus a table of estimated times for your chosen n.

Can it be wrong?

Yes, for loops with unusual bounds, early exits or hidden costs in function calls. Read the explanation and check it against the code.

Totalment gratisSense cost, sense registre
Els fitxers es mantenen privatsTot funciona al teu navegador
Fàcil en el teu idiomaDisponible en molts idiomes