0
0
Data Structures Theoryknowledge~10 mins

Common complexity classes (O(1), O(n), O(log n), O(n²)) in Data Structures Theory - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Common complexity classes (O(1), O(n), O(log n), O(n²))
Start: Input size n
Choose algorithm
Measure steps as function of n
Classify complexity
O(1): Constant steps
O(log n): Steps grow slowly
O(n): Steps grow linearly
O(n²): Steps grow quadratically
Predict performance for large n
End
This flow shows how we start with input size, pick an algorithm, count steps, classify complexity, and predict performance.
Execution Sample
Data Structures Theory
def example(n):
    for i in range(n):
        for j in range(n):
            print(i, j)
This code prints pairs (i, j) for all i and j from 0 to n-1, showing O(n²) complexity.
Analysis Table
StepijCondition i<nCondition j<nActionOutput
1000<3 True0<3 TruePrint (0,0)(0,0)
2010<3 True1<3 TruePrint (0,1)(0,1)
3020<3 True2<3 TruePrint (0,2)(0,2)
4030<3 True3<3 FalseInner loop ends
5101<3 True0<3 TruePrint (1,0)(1,0)
6111<3 True1<3 TruePrint (1,1)(1,1)
7121<3 True2<3 TruePrint (1,2)(1,2)
8131<3 True3<3 FalseInner loop ends
9202<3 True0<3 TruePrint (2,0)(2,0)
10212<3 True1<3 TruePrint (2,1)(2,1)
11222<3 True2<3 TruePrint (2,2)(2,2)
12232<3 True3<3 FalseInner loop ends
133-3<3 False-Outer loop ends
💡 Outer loop i=3 fails condition i<n (3<3 is False), so loops end.
State Tracker
VariableStartAfter Step 1After Step 4After Step 8After Step 12Final
i-00123
j-0333-
Key Insights - 3 Insights
Why does the inner loop reset j to 0 after it reaches n?
Because after j reaches n and the inner loop ends (see Step 4, 8, 12), the outer loop increments i and the inner loop starts again with j=0 (Step 5, 9). This is why j resets each time.
Why is this algorithm O(n²) and not O(n)?
Because for each of the n iterations of i, the inner loop runs n times, making total steps about n * n = n² (see execution_table rows 1-12).
What would happen if we removed the inner loop?
Then the steps would be just n (one loop), making complexity O(n). The execution_table would have fewer rows and j would not change.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 7. What are the values of i and j?
Ai=0, j=2
Bi=1, j=2
Ci=2, j=1
Di=1, j=1
💡 Hint
Check the 'i' and 'j' columns in execution_table row for Step 7.
At which step does the outer loop condition become false?
AStep 12
BStep 4
CStep 13
DStep 1
💡 Hint
Look at the 'Condition i
If n was increased to 4, how many total print actions would occur?
A16
B8
C12
D4
💡 Hint
Recall that total prints = n * n, see execution_table steps 1-12 for n=3.
Concept Snapshot
Common complexity classes:
O(1): Constant steps, no matter input size.
O(n): Steps grow linearly with input size.
O(log n): Steps grow slowly, halving input each time.
O(n²): Steps grow quadratically, nested loops.
Used to predict algorithm speed as input grows.
Full Transcript
This visual execution shows how algorithms behave with different input sizes n. We start with input size n, pick an algorithm, and count how many steps it takes. For example, nested loops cause steps to grow as n squared, shown by printing pairs (i, j) for i and j from 0 to n-1. The execution table traces each step, showing variable values and conditions. The variable tracker shows how i and j change over time. Key moments clarify why loops reset and why complexity is quadratic. The quiz tests understanding of variable values and loop endings. The snapshot summarizes common complexity classes and their meaning.