Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST TOOLS

C Tutorial

C HOME C Intro C Get Started C Syntax C Output C Comments C Variables C Data Types C Type Conversion C Constants C Operators C Booleans C If...Else C Switch C While Loop C For Loop C Break/Continue C Arrays C Strings C User Input C Memory Address C Pointers

C Functions

C Functions C Function Parameters C Scope C Function Declaration C Functions Challenge C Math Functions C Inline Functions C Recursion C Function Pointers

C Files

C Create Files C Write To Files C Read Files

C Structures

C Structures C Structs Challenge C Nested Structures C Structs & Pointers C Unions C typedef C Struct Padding

C Enums

C Enums

C Memory

C Memory Management

C Errors

C Errors C Error Challenge C Debugging C NULL C Error Handling C Input Validation

C More

C Date C Random Numbers C Macros C Organize Code C Storage Classes C Bitwise Operators C Fixed-width Integers

C DSA

C DSA Intro C Searching C Sorting C Stacks C Queues C Linked Lists C Trees C Big O

C Projects

C Projects

C Cert

C Certificate

C Reference

C Reference C Keywords C <stdio.h> C <stdlib.h> C <string.h> C <math.h> C <ctype.h> C <time.h>

C Examples

C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Practice Problems C Compiler C Syllabus C Study Plan C Interview Q&A

C DSA Big O Notation

Share

What is Big O?

Big O describes how the work an algorithm does grows when the amount of data grows.

  • The amount of data is called n. For example, the number of items in an array.
  • Big O is written with a big letter O, like O(n). You say it like this: "Big O of n".
  • Big O does not care about exact seconds. It cares about the shape of the growth: what happens when n gets 10 or 1,000 times bigger?

Count the Steps

In the Searching chapter, binary search was much faster than linear search. Let's count exactly how many checks each one needs in the worst case.

This program does not search anything. It only counts the steps for different sizes of n:

Example

#include <stdio.h>

int main() {
  int sizes[] = {10, 100, 1000, 1000000};

  for (int i = 0; i < 4; i++) {
    int n = sizes[i];

    // Linear search: check every item
    int linearSteps = n;

    // Binary search: cut the items in half until none are left
    int binarySteps = 0;
    int items = n;
    while (items > 0) {
      items = items / 2;
      binarySteps++;
    }

    printf("n = %d: linear = %d steps, binary = %d steps\n", n, linearSteps, binarySteps);
  }

  return 0;
}

Result:

n = 10: linear = 10 steps, binary = 4 steps
n = 100: linear = 100 steps, binary = 7 steps
n = 1000: linear = 1000 steps, binary = 10 steps
n = 1000000: linear = 1000000 steps, binary = 20 steps
Try it Yourself »
  • Linear search grows at the same speed as n. When n gets 1,000 times bigger, the steps get 1,000 times bigger. We write this as O(n).
  • Binary search grows very slowly. When n doubles, it needs only one more step. We write this as O(log n).

"log n" (logarithm) sounds like hard math, but here it just means: "how many times can you cut n in half?"


The Most Common Big O Values

Here are the Big O values you will see most often, from fastest to slowest:

Big O Steps When n = 1,000 Example
O(1) 1 Push onto a stack
O(log n) about 10 Binary search
O(n) 1,000 Linear search
O(n log n) about 10,000 qsort()
O(n2) 1,000,000 Bubble sort

Look at the difference! With 1,000 items, an O(n2) algorithm can need a million steps, while an O(log n) algorithm needs about 10.



Spot the Big O in Your Code

You can often find the Big O just by looking at the loops.

O(1): No Loop

The same number of steps, no matter how big the array is. This is called constant time:

Example

int first = numbers[0];

O(n): One Loop

The loop runs once for every item. This is called linear time:

Example

for (int i = 0; i < n; i++) {
  printf("%d\n", numbers[i]);
}

O(n2): A Loop Inside a Loop

For every item, the inner loop runs through all items again. That is n × n steps, and it is called quadratic time:

Example

for (int i = 0; i < n; i++) {
  for (int j = 0; j < n; j++) {
    printf("%d %d\n", numbers[i], numbers[j]);
  }
}

O(log n): Cut in Half

The work is cut in half at every step. This is called logarithmic time:

Example

while (n > 0) {
  n = n / 2;
}

Cheat Sheet: This Section in Big O

Here is everything you have learned in this section, with its Big O:

What You Do Big O
Linear searchO(n)
Binary searchO(log n)
Bubble sortO(n2)
qsort()O(n log n) (usually)
Stack: push or popO(1)
Queue: enqueue or dequeueO(1)
Linked list: insert at the frontO(1)
Linked list: find or delete a valueO(n)
Binary search tree: insert or search (well-shaped tree)O(log n)

Summary

  • Big O describes how the number of steps grows when the data (n) grows.
  • From fast to slow: O(1), O(log n), O(n), O(n log n), O(n2).
  • Look at the loops: one loop is often O(n), a loop inside a loop is often O(n2).

Well Done!

You have built stacks, queues, linked lists and trees from scratch in C. You can search and sort, and you know how to measure how fast an algorithm is. That is a great start in DSA!


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.