100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C

Time Complexity in C

Understand Big-O notation in C with C code examples for O(1), O(log n), O(n), O(n log n), O(n^2), and O(2^n) growth rates.

Algorithms & Interview PrepIntermediate12 min readJul 7, 2026
Analogies

1. Introduction

Time complexity describes how the running time of an algorithm grows as the size of its input, usually denoted n, increases. Rather than measuring exact seconds (which depends on hardware, compiler, and system load), time complexity abstracts away those details and expresses growth using Big-O notation, which captures the upper bound of an algorithm's growth rate for large n. Understanding time complexity is essential for writing efficient C programs and is a core topic in technical interviews, since it lets you predict how an algorithm will scale before you even run it.

🏏

Cricket analogy: Time complexity is like judging a bowler's economy rate rather than the exact clock time of an over, since Big-O abstracts away pitch conditions and crowd noise to predict how performance scales as overs increase.

2. Syntax

text
Big-O notation is written as O(f(n)), where f(n) describes
how the number of operations grows relative to input size n.

Common complexity classes, from fastest to slowest growth:
  O(1)        - constant time
  O(log n)    - logarithmic time
  O(n)        - linear time
  O(n log n)  - linearithmic time
  O(n^2)      - quadratic time
  O(2^n)      - exponential time

3. Explanation

Big-O notation focuses on the dominant term as n grows large and ignores constant factors and lower-order terms, since those matter less as n approaches infinity. For example, an algorithm that does 3n + 5 operations is still O(n), because the constant 3 and the additive 5 become insignificant compared to n for very large inputs. Below is a code example for each major complexity class, with a short explanation of why it falls into that class.

🏏

Cricket analogy: Big-O ignoring constants is like saying a batsman's strike rate matters more than a few extra dot balls at the start of an innings — 3n+5 balls faced is still 'O(n) balls' since the +5 warm-up barely matters over a long knock.

O(1) — Constant Time

An O(1) operation takes the same amount of time regardless of input size, such as accessing an array element by index.

🏏

Cricket analogy: O(1) is like a scorer instantly reading a specific over's total from a numbered scoreboard slot without scanning every over — accessing an array element by index is exactly this constant-time lookup.

c
int getFirstElement(int arr[], int n) {
    return arr[0]; /* one operation, regardless of how large n is */
}

O(log n) — Logarithmic Time

An O(log n) algorithm reduces the problem size by a constant factor (commonly half) at each step, such as binary search.

🏏

Cricket analogy: O(log n) is like a selector narrowing down a 128-player trial pool to the final squad by cutting it in half at each round of evaluation, just like binary search halving the search space every step.

c
int binarySearch(int arr[], int low, int high, int key) {
    while (low <= high) {
        int mid = low + (high - low) / 2;
        if (arr[mid] == key) return mid;
        else if (arr[mid] < key) low = mid + 1;
        else high = mid - 1;
    }
    return -1; /* search range halves each iteration -> O(log n) */
}

O(n) — Linear Time

An O(n) algorithm visits every element of the input exactly once, such as finding the maximum value in an array.

🏏

Cricket analogy: O(n) is like a scorer reading every single ball of an innings once to find the highest-scoring over — you must check each one exactly once, so time grows directly with the number of balls bowled.

c
int findMax(int arr[], int n) {
    int max = arr[0];
    for (int i = 1; i < n; i++) {   /* single pass over n elements */
        if (arr[i] > max) max = arr[i];
    }
    return max;
}

O(n log n) — Linearithmic Time

This complexity typically arises from divide-and-conquer algorithms that split the input log n times and do O(n) work at each level, such as merge sort.

🏏

Cricket analogy: O(n log n) is like organizing a league table by repeatedly splitting groups of teams in half to rank them, then merging the sorted groups back together — just like merge sort splitting log n times and merging O(n) work each level.

c
void mergeSort(int arr[], int left, int right) {
    if (left < right) {
        int mid = left + (right - left) / 2;
        mergeSort(arr, left, mid);      /* recurse on left half  */
        mergeSort(arr, mid + 1, right); /* recurse on right half */
        merge(arr, left, mid, right);   /* O(n) merge at each of log n levels */
    }
}

O(n^2) — Quadratic Time

An O(n^2) algorithm typically has nested loops where each loop runs proportional to n, such as bubble sort or checking all pairs of elements.

🏏

Cricket analogy: O(n^2) is like comparing every player on a squad against every other player to rank head-to-head stats, a nested-loop check similar to bubble sort's repeated pairwise comparisons across the whole team.

c
void bubbleSort(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - 1 - i; j++) {  /* nested loop -> n * n work */
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

O(2^n) — Exponential Time

An O(2^n) algorithm's work doubles with every additional input element, commonly seen in naive recursive solutions like computing Fibonacci numbers without memoization.

🏏

Cricket analogy: O(2^n) is like a knockout prediction bracket where every extra match doubles the number of possible outcome paths to check, similar to naive recursive Fibonacci recomputing the same subproblems exponentially without memoization.

c
int fib(int n) {
    if (n <= 1) return n;
    return fib(n - 1) + fib(n - 2); /* each call spawns 2 more calls -> O(2^n) */
}

4. Example

c
#include <stdio.h>

int getFirstElement(int arr[], int n) { return arr[0]; }

int findMax(int arr[], int n) {
    int max = arr[0];
    for (int i = 1; i < n; i++) {
        if (arr[i] > max) max = arr[i];
    }
    return max;
}

int main(void) {
    int arr[] = {4, 2, 9, 1, 7};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("O(1) first element: %d\n", getFirstElement(arr, n));
    printf("O(n) max element: %d\n", findMax(arr, n));
    return 0;
}

5. Output

text
O(1) first element: 4
O(n) max element: 9

6. Key Takeaways

  • Growth order from fastest to slowest: O(1) < O(log n) < O(n) < O(n log n) < O(n^2) < O(2^n).
  • O(1): array indexing, hash table lookup (average case), pushing to a fixed-size stack.
  • O(log n): binary search, balanced binary search tree operations.
  • O(n): linear search, single-pass array traversal (sum, max, min).
  • O(n log n): merge sort, quick sort (average case), heap sort.
  • O(n^2): bubble sort, insertion sort, selection sort, naive nested-loop pair comparisons.
  • O(2^n): naive recursive Fibonacci, generating all subsets of a set (power set).

Practice what you learned

Was this page helpful?

Topics covered

#CProgrammingStudyNotes#Programming#TimeComplexityInC#Time#Complexity#Syntax#Explanation#Algorithms#StudyNotes#SkillVeris

Frequently Asked Questions

21 categories · pick one to explore

Where can I get free study notes for programming and tech subjects?
SkillVeris offers completely free study notes covering programming and tech subjects, with no signup fees or paywalls. The notes are structured by course and topic, written for quick understanding, and enriched with the Learn Through Hobbies analogy method, so you can revise concepts through cricket, music, gaming, cooking and more.
Are SkillVeris study notes good for exam revision?
Yes, the study notes are designed for efficient revision: each topic answers its heading immediately, keeps explanations concise, and links to related glossary terms and cheat sheets. Students preparing for university exams or certification tests use them as quick revision notes because they distil concepts without the padding of full textbooks.
What subjects do the free study notes cover?
The study notes span the platform's main domains, including AI and machine learning, Python and programming, web development, DevOps, cloud, security and databases. Coverage mirrors the 37 live courses, so notes exist for the topics you are actually studying, and new note sets are added as courses launch.
How are SkillVeris study notes different from regular textbooks?
The notes are answer-first, concise and free, whereas textbooks are long and often expensive. Each section explains one concept directly, then reinforces it through selectable hobby analogies like cricket or cooking. Notes also cross-link to the glossary, blog and cheat sheets, letting you jump to related material instantly instead of flipping pages.
Can I use the developer study material without creating an account?
The study notes are free to access, and SkillVeris does not charge anything for its developer study material at any point. Browsing notes is straightforward from the Study Notes section, and if you want progress tracking, certificates and AI Mentor conversations tied to your learning, a free account unlocks those extras.
Do the study notes explain concepts with analogies?
Yes, this is a signature SkillVeris feature. Study notes use the Learn Through Hobbies method, explaining technical concepts through analogies from twelve domains including cricket, music, gaming, photography, travel, movies, fitness, chess, cooking, finance, business and sports. You can switch the analogy domain instantly to whichever hobby makes the concept click.
Are the revision notes suitable for last-minute exam preparation?
Yes, revision notes on SkillVeris work well for last-minute preparation because every section states the answer in its first sentences, so skimming is genuinely effective. Pair them with the relevant cheat sheet for formulas and syntax, and use the glossary for any unfamiliar term you meet while cramming.
Is there free study material for AI and machine learning?
Yes, SkillVeris provides free study notes across its AI and ML catalogue, covering Python for AI, deep learning frameworks like PyTorch and TensorFlow, Hugging Face Transformers, Large Language Models, RAG, AI agents and MLOps. All of it is free, making it a strong resource for Indian students and global learners alike.
Can beginners understand the study notes, or are they for experts?
Beginners can absolutely use them. The notes are written in plain language, define terms as they appear, and lean on hobby analogies to make abstract ideas concrete. Difficulty scales with the underlying course level, so beginner-course notes stay gentle while advanced-course notes go deeper, and the glossary supports you throughout.
How do study notes connect with SkillVeris courses?
Study notes are organised by course and topic, so they map directly to the structured courses and their 24–40-lesson curriculum. Many learners study a lesson first, then use the matching notes for revision before module assessments and the final exam, where 80 percent is required to pass and earn the certificate.
Are there study notes for Python specifically?
Yes, Python is well covered through notes tied to the Python-focused courses, including Python for AI and ML. Topics span fundamentals through applied machine learning usage. You can reinforce the notes with Python practice in Code Lab, which runs code in your browser with no installation required.
Do the study notes include code examples?
Yes, study notes include code examples wherever a concept is best shown in code, alongside explanations, key points and analogies. Reading a snippet in the notes and then reproducing it yourself in Code Lab is an effective loop, since Code Lab lets you run code in the browser across six languages.
How often is new study material added to SkillVeris?
Study material grows alongside the course catalogue. Whenever new courses join the platform's 37 live courses, matching study notes, glossary entries and cheat sheets are added so the resources stay in sync. Existing notes are also refined over time, so it is worth revisiting topics you studied earlier.
Can I use SkillVeris notes to prepare for technical interviews?
Yes, the notes make excellent interview revision because they compress each concept into direct, answer-first explanations, which mirrors how you should answer interview questions. Combine them with the SkillVeris interview questions feature, which includes readiness scoring, to test whether your revision has actually made you interview-ready.
Are the study notes mobile-friendly for studying on the go?
Yes, the study notes are built to load fast and read comfortably on mobile devices, so you can revise during a commute or between classes. Sections are short and answer-first, which suits small screens, and analogy switching works on mobile too, letting you study anywhere without carrying books.
What is the difference between study notes and cheat sheets?
Study notes explain concepts in depth with context, examples and analogies, making them ideal for learning and revision. Cheat sheets are compact quick-reference summaries of syntax, commands and key facts, ideal once you already understand a topic. Most learners study the notes first, then keep the cheat sheet handy while coding.
Do study notes help if I am stuck on a course lesson?
Yes, reading the matching study notes often clarifies a lesson because the same concept is explained from a different angle, frequently with a different analogy. If you are still stuck, ask the AI Mentor, which answers 24/7 at Quick, Detailed or Deep-dive depth until the idea genuinely makes sense.
Is there free study material for DevOps and cloud topics?
Yes, SkillVeris carries free study notes for DevOps and cloud topics as part of its coverage across 37 live courses. The material suits learners following the DevOps Engineer or Cloud Engineer paths, and it links to related glossary terms and cheat sheets so you can revise the whole toolchain in one place.
Can school or college students in India use these notes for projects?
Yes, students across India and worldwide use SkillVeris notes for coursework, projects and exam preparation, and everything is free, which matters for student budgets. The notes explain concepts clearly enough to cite in project reports, and Code Lab lets you prototype the project code directly in your browser.
How should I combine study notes with other SkillVeris resources?
A proven loop: learn from a course lesson, revise with the matching study notes, look up unfamiliar terms in the glossary, keep the cheat sheet open while practising in Code Lab, and quiz yourself with interview questions. The AI Mentor fills any remaining gaps 24/7, at whatever depth you need.

What Learners Say

Real journeys from the SkillVeris community — swipe for more.

SkillVeris taught me Python through Cricket. Now I’m building real projects and feeling confident!
Arjun S. · B.Tech Student
The best platform for hobby-based learning. Concepts finally stick.
Priya R. · Data Analyst
I went from zero coding to a portfolio of projects — all by learning through my love for gaming. Landed my first internship!
Kabir M. · CS Undergraduate
Trending Topics50 popular tags — tap to explore
Trending CoursesAll 37 free courses — tap to browse