🧮 JavaScript Data Structures & Algorithms
Estimated reading: 4 minutes 8 views

⚙️ JavaScript – Algorithms: Sorting & Recursion Explained with Examples


🧲 Introduction – Why Sorting and Recursion Matter in JavaScript

Have you ever wondered how search engines sort millions of results or how games navigate complex maps? The secret lies in algorithms—well-defined steps that solve problems efficiently.

Among the most critical algorithmic concepts in JavaScript are:

  • 🔄 Sorting – For organizing data efficiently
  • 🔁 Recursion – For solving problems by breaking them into smaller sub-problems

In this article, you’ll learn:

✅ How common sorting algorithms work in JavaScript
✅ The power and pitfalls of recursion
✅ Real-world use cases and implementation examples
✅ Best practices and performance insights


🔃 1. Sorting Algorithms in JavaScript

Sorting is the backbone of many applications—from filtering user data to search functionality.

📌 Built-in JavaScript .sort() Method

const nums = [4, 2, 9, 1];
nums.sort((a, b) => a - b);
console.log(nums); // [1, 2, 4, 9]

Explanation:

  • .sort() rearranges array elements in-place.
  • (a, b) => a - b is a compare function for ascending order.

⚠️ Warning: Without a compare function, .sort() converts items to strings—causing unexpected results for numbers.


📊 Bubble Sort (Beginner-Friendly)

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

console.log(bubbleSort([3, 1, 4, 2])); // [1, 2, 3, 4]

Explanation:

  • Compares adjacent pairs and swaps if out of order.
  • Repeats until the list is sorted.

📘 Time Complexity: O(n²) – inefficient for large data.


⚡ Merge Sort (Efficient & Recursive)

function mergeSort(arr) {
  if (arr.length < 2) return arr;
  const mid = Math.floor(arr.length / 2);
  const left = mergeSort(arr.slice(0, mid));
  const right = mergeSort(arr.slice(mid));
  
  return merge(left, right);
}

function merge(left, right) {
  const result = [];
  while (left.length && right.length) {
    result.push(left[0] < right[0] ? left.shift() : right.shift());
  }
  return result.concat(left, right);
}

console.log(mergeSort([4, 2, 1, 3])); // [1, 2, 3, 4]

Explanation:

  • Divides the array into halves recursively.
  • Merges sorted halves.

💡 Time Complexity: O(n log n) – efficient and stable.


🔥 Quick Sort (Divide & Conquer)

function quickSort(arr) {
  if (arr.length < 2) return arr;
  const pivot = arr[0];
  const left = arr.slice(1).filter(el => el < pivot);
  const right = arr.slice(1).filter(el => el >= pivot);
  return [...quickSort(left), pivot, ...quickSort(right)];
}

console.log(quickSort([5, 3, 8, 1])); // [1, 3, 5, 8]

Explanation:

  • Picks a pivot, then splits into left/right parts.
  • Recursively sorts and merges.

📘 Time Complexity: Average O(n log n), Worst O(n²) (when poorly partitioned)


🔁 2. Recursion in JavaScript

📌 What is Recursion?

Recursion is when a function calls itself to solve a smaller piece of a problem.


🧮 Factorial Example (Classic Recursive Function)

function factorial(n) {
  if (n === 0) return 1;
  return n * factorial(n - 1);
}

console.log(factorial(5)); // 120

Explanation:

  • Base case: n === 0 returns 1.
  • Recursive case: multiply n by factorial(n - 1).

💡 Use When: The problem can be broken into identical smaller sub-problems.


🧵 Fibonacci Sequence (Recursive)

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

console.log(fib(5)); // 5

⚠️ Note: This version is inefficient (O(2^n)). Use memoization to optimize.


🧠 Optimized Fibonacci with Memoization

function fibMemo(n, memo = {}) {
  if (n in memo) return memo[n];
  if (n <= 1) return n;
  return memo[n] = fibMemo(n - 1, memo) + fibMemo(n - 2, memo);
}

console.log(fibMemo(40)); // Fast computation

Memoization stores previously computed results to avoid redundant work.

📘 Time Complexity: Optimized to O(n)


🧠 Recursive vs Iterative Comparison

FeatureRecursionIteration
SimplicityClean, elegant for nested problemsMore efficient for large inputs
Memory usageHigh (due to call stack)Low
PerformanceMay be slower without memoFaster
Use CaseTree traversal, backtrackingLoops, linear tasks

✅ Best Practices for Algorithms in JavaScript

  • ✅ Use built-in methods like .sort() when performance isn’t critical.
  • ✅ Prefer merge sort or quick sort for large arrays.
  • ✅ Avoid deep recursion without base cases or memoization.
  • ✅ Use tail recursion optimization if supported (not always in JS engines).
  • ✅ Benchmark critical algorithms with console.time().

📌 Summary

In this guide, we covered:

  • 📊 Popular sorting algorithms: Bubble, Merge, Quick
  • 🔁 Key recursive concepts and optimized examples
  • 📈 Best practices to improve performance and clarity

Understanding these concepts makes you a better problem solver and helps build faster, scalable applications in JavaScript.


❓FAQs – JavaScript Algorithms (Sort & Recursion)

Which sorting algorithm is best in JavaScript?
➡️ Use built-in .sort() for most needs. For stability and large data, mergeSort is reliable. quickSort is fast but may degrade in worst cases.

Why is recursion risky in JavaScript?
➡️ Recursive functions can cause stack overflows if not properly terminated or optimized. Deep recursion can be costly in terms of memory.

Is recursion faster than iteration?
➡️ Generally no—iteration is faster and more memory-efficient. Recursion is more readable in problems like trees or backtracking.

What is memoization?
➡️ Memoization is an optimization where results of expensive function calls are cached and reused to avoid redundant work.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

JavaScript — Algorithms (Sort, Recursion)

Or Copy Link

CONTENTS
Scroll to Top