Sorting is one of the most common problems in computer science, and interviewers love it because it's a great window into how you think about trade-offs. Here are the three sorting algorithms you'll run into most often, explained simply, with working JavaScript code.
1. Bubble Sort — The Simplest One
Bubble sort repeatedly walks through the list, comparing neighbors and swapping them if they're in the wrong order. Bigger values slowly "bubble" toward the end.
function bubbleSort(arr) {
const a = [...arr];
for (let i = 0; i < a.length; i++) {
for (let j = 0; j < a.length - i - 1; j++) {
if (a[j] > a[j + 1]) {
[a[j], a[j + 1]] = [a[j + 1], a[j]]; // swap
}
}
}
return a;
}
console.log(bubbleSort([5, 3, 8, 1, 2])); // [1, 2, 3, 5, 8]
Time complexity: O(n²) — easy to understand, but too slow for large lists. Mostly used for teaching, not production.
2. Merge Sort — Divide and Conquer
Merge sort splits the list in half repeatedly until each piece has just one element (already "sorted" by definition), then merges those pieces back together in sorted order.
function mergeSort(arr) {
if (arr.length <= 1) 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 = [];
let i = 0, j = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) result.push(left[i++]);
else result.push(right[j++]);
}
return [...result, ...left.slice(i), ...right.slice(j)];
}
console.log(mergeSort([5, 3, 8, 1, 2])); // [1, 2, 3, 5, 8]
Time complexity: O(n log n) in every case — reliably fast, and the standard example of "divide and conquer" thinking.
3. Quick Sort — Fast in Practice
Quick sort picks a "pivot" value, moves everything smaller to its left and everything bigger to its right, then repeats that process on each side.
function quickSort(arr) {
if (arr.length <= 1) return arr;
const [pivot, ...rest] = arr;
const left = rest.filter(n => n < pivot);
const right = rest.filter(n => n >= pivot);
return [...quickSort(left), pivot, ...quickSort(right)];
}
console.log(quickSort([5, 3, 8, 1, 2])); // [1, 2, 3, 5, 8]
Time complexity: O(n log n) on average, but O(n²) in the worst case (e.g., an already-sorted array with a poorly chosen pivot). In practice it's usually the fastest general-purpose sort, which is why it backs many built-in sort() implementations.
Which One Should You Use?
In real JavaScript code, just use the built-in
Array.prototype.sort()— modern engines use a highly-optimized hybrid algorithm under the hood.Know bubble sort to explain the basic idea of comparison-based sorting.
Know merge sort when consistent
O(n log n)performance matters, or when sorting linked lists.Know quick sort as the go-to fast, in-place, general-purpose sort.
Conclusion
That wraps up this 5-part DSA series: Big O Notation, Arrays vs Linked Lists, Stacks and Queues, Binary Search, and now Sorting Algorithms. Together these cover the foundation that almost every other data structure and algorithm topic builds on — trees, graphs, dynamic programming, and more.
Image: Swfung8 / Wikimedia Commons (CC BY-SA 3.0)

Comments
Post a Comment