DevPath · Learn to code ESPTEN

Algorithms and complexity

Sorting and recursion

Sorting

Bubble sort — O(n²)

The simplest to understand: it compares adjacent pairs and swaps them if they are in the wrong order, repeating until there are no changes.

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

Merge sort and quick sort — O(n log n)

For large data, divide-and-conquer algorithms are used:

Both are much faster than bubble sort for large inputs. In practice, Array.prototype.sort() already uses an efficient algorithm.

Recursion

A recursive function calls itself with a smaller problem until it reaches a base case.

function factorial(n) {
  if (n <= 1) return 1;       // base case
  return n * factorial(n - 1); // recursive case
}

function fibonacci(n) {
  if (n < 2) return n;
  return fibonacci(n - 1) + fibonacci(n - 2);
}

The naive recursive Fibonacci is O(2ⁿ): it recomputes the same values over and over. It can be optimized with memoization or with a loop.

Examples

Bubble sort with trace

function bubbleSort(arr) {
  const a = [...arr];
  for (let i = 0; i < a.length; i++) {
    for (let j = 0; j < a.length - 1 - i; j++) {
      if (a[j] > a[j + 1]) [a[j], a[j + 1]] = [a[j + 1], a[j]];
    }
  }
  return a;
}
console.log(bubbleSort([5, 2, 8, 1, 9, 3]));

Recursive factorial

function factorial(n) {
  if (n <= 1) return 1;
  return n * factorial(n - 1);
}
console.log("5! =", factorial(5));
Put this into practice

DevPath is a hands-on course: you read the theory here; in the app you put it into practice with exercises that really run, offline.

Start free in the app →
← Linear and binary searchMerge sort in detail →