Back to Notes
Computer Science2024-11-20

Algorithmic Thinking & Big-O Complexity

Analyzing algorithm efficiency is essential for building scalable applications. We use asymptotic notations to describe computational complexity.

Big-O Notation

Big-O describes the upper bound of the running time or space complexity of an algorithm in the worst-case scenario.

Complexity Comparison Table

ComplexityNameExample Algorithm
O(1)O(1)ConstantArray Index Access
O(logn)O(\log n)LogarithmicBinary Search
O(n)O(n)LinearSingle Loop
O(nlogn)O(n \log n)LinearithmicMerge Sort / QuickSort
O(n2)O(n^2)QuadraticBubble Sort / Selection Sort

Here is a sample implementation of Binary Search in TypeScript:

function binarySearch(arr: number[], target: number): number {
  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; // Not found
}