DEV Community

Cover image for Day 2: Understanding Big O Notation πŸ“Š
Dipak Ahirav
Dipak Ahirav

Posted on • Edited on

Day 2: Understanding Big O Notation πŸ“Š

Welcome to Day 2 of our Data Structures and Algorithms (DSA) series! Yesterday, we introduced the basics of DSA. Today, we'll dive deeper into Big O Notation, a fundamental concept for analyzing the efficiency of algorithms. Let’s get started! πŸš€

please subscribe to my YouTube channel to support my channel and get more web development tutorials.

What is Big O Notation? πŸ€”

Big O Notation is a mathematical notation used to describe the upper bound of an algorithm's runtime or space requirements in terms of input size. It helps us understand the worst-case scenario for the performance of an algorithm, providing a way to measure and compare the efficiency of different algorithms.

Why is Big O Important?

  • Performance Analysis: Helps in evaluating the efficiency of algorithms.
  • Scalability: Determines how well an algorithm performs as the input size grows.
  • Optimization: Guides developers in writing optimized code.

Common Big O Notations and Their Meanings πŸ“ˆ

O(1) - Constant Time

An algorithm with O(1) complexity performs a task in constant time, regardless of the input size.
Example: Accessing an element in an array by index.

function getFirstElement(arr) {
    return arr[0];
}
Enter fullscreen mode Exit fullscreen mode

O(n) - Linear Time

An algorithm with O(n) complexity has a runtime that grows linearly with the input size.
Example: Looping through an array.

function printAllElements(arr) {
    arr.forEach(element => {
        console.log(element);
    });
}
Enter fullscreen mode Exit fullscreen mode

O(log n) - Logarithmic Time

An algorithm with O(log n) complexity has a runtime that grows logarithmically as the input size increases. Typically seen in algorithms that halve the input size at each step.
Example: Binary Search.

function binarySearch(arr, target) {
    let left = 0, right = arr.length - 1;
    while (left <= right) {
        let mid = Math.floor((left + right) / 2);
        if (arr[mid] === target) {
            return mid;
        } else if (arr[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return -1;
}
Enter fullscreen mode Exit fullscreen mode

O(n^2) - Quadratic Time

An algorithm with O(n^2) complexity has a runtime that grows quadratically with the input size. Commonly seen in nested loops.
Example: Bubble Sort.

function bubbleSort(arr) {
    let n = arr.length;
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Other Common Notations

  • O(n log n): Seen in efficient sorting algorithms like Merge Sort and Quick Sort.
  • O(2^n): Exponential time, often found in recursive algorithms that solve a problem of size n by solving two smaller problems of size n-1.

Analyzing Time Complexity πŸ“

When analyzing an algorithm, consider the following steps:

  1. Identify the input size (n).
  2. Determine the basic operations performed (e.g., comparisons, assignments).
  3. Count the number of basic operations in terms of n.
  4. Express the time complexity using Big O notation.

Example Analysis

Let’s analyze the time complexity of a simple function that prints pairs of elements in an array.

function printPairs(arr) {
    for (let i = 0; i < arr.length; i++) {
        for (let j = 0; j < arr.length; j++) {
            console.log(arr[i], arr[j]);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  1. Input size (n): Length of the array.
  2. Basic operations: Print statements.
  3. Number of operations: Two nested loops, each running n times, resulting in n * n operations.
  4. Time complexity: O(n^2).

Conclusion 🎯

Understanding Big O Notation is crucial for analyzing the efficiency of algorithms. It provides a standard way to express the performance and helps in comparing different algorithms. Today, we covered the most common Big O notations and how to analyze time complexity.

Series Index

Part Title Link
1 Day 1: Introduction to Data Structures and Algorithms (DSA)πŸš€ Read
2 Day 2: Understanding Big O Notation πŸ“Š Read
3 Day 3: Introduction to Arrays πŸ“‹ Read
4 Day 4: Understanding Linked Lists πŸ”— Read

Stay tuned for Day 3, where we will explore Arrays, their properties, and common operations. Feel free to share your thoughts or questions in the comments below. Happy coding! πŸ’»


Feel free to leave your comments or questions below. If you found this guide helpful, please share it with your peers and follow me for more web development tutorials. Happy coding!

Follow and Subscribe:

Top comments (0)