Cal11 calculator

Find The Time Complexity of The Following Code Calculator

Reviewed by Calculator Editorial Team

Determine the time complexity of code segments with our interactive calculator. This tool helps you analyze algorithm efficiency by examining loops, nested structures, and recursive calls. Learn how to identify Big-O notation through practical examples and step-by-step guidance.

How to Use This Calculator

To find the time complexity of your code:

  1. Paste your code snippet into the input box
  2. Select the programming language (currently supports JavaScript, Python, and Java)
  3. Click "Calculate" to analyze the code
  4. Review the time complexity result and detailed breakdown

This calculator analyzes basic control structures. For complex algorithms, manual analysis may be needed.

Time Complexity Basics

Time complexity describes how an algorithm's runtime grows with input size. It's expressed using Big-O notation:

O(1) - Constant time
O(log n) - Logarithmic time
O(n) - Linear time
O(n log n) - Linearithmic time
O(n²) - Quadratic time
O(2ⁿ) - Exponential time

The most dominant term determines the complexity. For example, a function with both O(n) and O(n²) operations is O(n²).

Common Code Patterns

Single Loop

Code with a single loop through an array is typically O(n).

for (let i = 0; i < arr.length; i++) {
  // O(1) operations
}

Nested Loops

Nested loops multiply their complexities. Two O(n) loops become O(n²).

for (let i = 0; i < n; i++) {
  for (let j = 0; j < n; j++) {
    // O(1) operations
  }
}

Binary Search

Divide-and-conquer algorithms like binary search are O(log n).

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

Worked Examples

Example 1: Linear Search

This simple search algorithm has O(n) time complexity.

function linearSearch(arr, target) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === target) return i;
  }
  return -1;
}

Example 2: Bubble Sort

This sorting algorithm has O(n²) time complexity due to nested loops.

function bubbleSort(arr) {
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr.length - i - 1; j++) {
      if (arr[j] > arr[j + 1]) {
        // Swap elements
      }
    }
  }
  return arr;
}

Frequently Asked Questions

What is the difference between time complexity and space complexity?
Time complexity measures runtime, while space complexity measures memory usage. Both are important for evaluating algorithm efficiency.
How do I determine the time complexity of recursive functions?
Count the number of recursive calls and their depth. For example, a function that halves the input size each call is O(log n).
What does O(1) mean?
O(1) means the algorithm takes constant time regardless of input size. Examples include array access or simple arithmetic operations.