Introduction to Algorithms

02 Mins

Data Structures (how we store data) and Algorithms (Series of steps to accomplish a task) form the backbone of computer science and programming.

Imagine you are searching for a word in a dictionary. Instead of checking every word one by one, you would flip to roughly middle of dictionary and see if word is before or after, and keep doing that until reach the word. That’s it, that is what Binary Search is.

binary-search

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; // Target found
    }

    if (arr[mid] < target) {
      left = mid + 1; // Search right half
    } else {
      right = mid - 1; // Search left half
    }
  }

  return -1; // Target not found
}

// Example
const numbers = [3, 7, 11, 16, 23, 27, 31, 42, 56];
console.log(binarySearch(numbers, 23)); // 4

Running Time and Big-O Notation

Algorithms grow at different rates. It’s not enough to know how long an algorithm takes to run, but we need to know how running time increases as input size increases.

That’s where Big-O notation comes handy. It tells how does a Algorithm perform in worst case.

For example: Linear Search may check n elements → O(n) while Binary Search repeatedly halves the search space → O(log n)

Why Big-O Matters ?

  • Helps compare algorithm efficiency.
  • Defines worst-case complexity.
  • Guides optimal solution selection.

Common complexities

Lets take a look at common algorithmic complexities.

asymptotic-notations-all-in-one-graph

Big O NotationNameExampleGrowth
O(1)ConstantAccessing an array elementDoesnt grow with n
O(log n)LogarithmicBinary SearchVery slow growth
O(n)LinearLinear SearchGrows proportionally
O(n log n)LinearithmicMerge/Quick SortFast
O(n²)QuadraticBubble/Selection SortGrows Quickly
O(2ⁿ)ExponentialGenerating all subsetsVery fast
O(n!)FactorialGenerating all permutationsExtremely fast