What is Time Complexity? Simple Examples

Time complexity is one of the most important concepts in DSA. It tells us how the running time of an algorithm changes when the input size gets bigger.

We don’t use exact seconds here β€” we care about the growth rate of the work done as input becomes large (like 10 β†’ 100 β†’ 10,000 β†’ 1 million).

Simple idea: Time complexity answers the question β€” β€œIf my data becomes 10Γ— bigger, how much slower (or faster) does my program become?”

Real-Life Analogies to Understand Time Complexity

1. Taking One Book From a Shelf πŸ“š

You go to a library and always take the first book from the shelf.

It doesn’t matter if the shelf has:

  • 10 books
  • 100 books
  • 10,000 books

Time taken is almost the same β€” you just pick the first one.

β†’ This is like a very fast algorithm whose time does not grow with input size.

2. Searching a Name in an Unsorted List πŸ”

You are searching for β€œRahul” in a list of names. You check one by one from start to end.

Rahul might be:

  • First name β†’ very fast
  • Middle β†’ medium time
  • Last name β†’ you had to check every single name

Worst case: You may need to look at the entire list.

β†’ More names = more time (time grows with input size).

3. Sorting Exam Papers by Marks πŸ“

A teacher wants to arrange 100 exam papers from lowest to highest marks.

They compare papers again and again to put them in correct order.

  • 10 papers β†’ manageable
  • 100 papers β†’ lots of comparisons
  • 1000 papers β†’ huge number of comparisons

β†’ When data grows, work grows much faster than the data itself.

Simple Code Examples to Feel Time Complexity

Example 1: Single Loop (Linear Growth)

for(int i = 0; i < n; i++) {
    cout << i << endl;
}

If n = 5 β†’ loop runs 5 times

If n = 100 β†’ loop runs 100 times

If n = 1,000,000 β†’ loop runs 1 million times

Idea: Work done is directly proportional to input size n.

Example 2: Two Separate Loops (Still Linear, but Twice the Work)

for(int i = 0; i < n; i++) {
    cout << "First loop: " << i << endl;
}

for(int j = 0; j < n; j++) {
    cout << "Second loop: " << j << endl;
}

If n = 5 β†’ total 10 steps (5 + 5)

If n = 100 β†’ total 200 steps (100 + 100)

Idea: Still grows linearly β€” just twice as much work, but same growth pattern.

Quick Summary – Rough Ideas Only (No Notations Yet)

  • Some algorithms take almost same time no matter how big input is
  • Some take time proportional to input size (like single loop)
  • Some become extremely slow when input grows (like bad sorting)
In the next article we will learn the standard way to express time complexity using notations:
O( ), Ξ©( ), Θ( ) β€” and understand them with clear examples.

Practice: Think of 2–3 daily tasks and decide β€” does time grow with more items or stay almost same?

Leave a Comment

Your email address will not be published.