Sorting FTW

Sorting algorithms can be broadly classified into two categories:

  1. Comparison-Based Sorting → These algorithms sort elements by comparing them (e.g., Quick Sort, Merge Sort, Heap Sort).
  2. Non-Comparison Sorting → These algorithms do not compare elements directly. Instead, they use properties like the digit structure of numbers or frequency counts.

Stable vs Non-stable

A stable sorting algorithm maintains the relative order of equal elements in the sorted output.

Stable Sorting → If two elements have the same value, their original order is preserved in the sorted result.

Unstable Sorting → The original order of equal elements may change.

Use a stable sort when:


Key Sorting algos

Sorting Algorithm Stable? Type Time Complexity (Best / Avg / Worst) Space Complexity In-Place?
Merge Sort ✅ Yes Comparison O(n log n) / O(n log n) / O(n log n) O(n) ❌ No
Quick Sort ❌ No Comparison O(n log n) / O(n log n) / O(n²) - if arr is already sorted O(log n) (due to recursion) ✅ Yes
Heap Sort ❌ No Comparison O(n log n) / O(n log n) / O(n log n) O(1) ✅ Yes
Bubble Sort ✅ Yes Comparison O(n) / O(n²) / O(n²) O(1) ✅ Yes
Insertion Sort ✅ Yes Comparison O(n) / O(n²) / O(n²) O(1) ✅ Yes
Counting Sort ✅ Yes Non-Comparison O(n + k) / O(n + k) / O(n + k) O(k) ❌ No
Radix Sort ✅ Yes Non-Comparison O(nk) / O(nk) / O(nk) O(n + k) ❌ No

💡 Key Observations

✔️ In-Place Sorting (✅ Yes): Uses O(1) or O(log n) extra space, modifying the original array (Quick Sort, Heap Sort, Bubble Sort, Insertion Sort).
✔️ Not In-Place (❌ No): Requires additional memory for merging or auxiliary structures (Merge Sort, Counting Sort, Radix Sort).
✔️ Merge Sort is stable but not in-place because it requires extra space for merging.
✔️ Quick Sort is in-place but not stable because swapping elements may change their relative order.
✔️ Counting and Radix Sort are stable but not in-place since they use extra memory to sort.


Further Notes

For FAANG interviews, you should focus on the following sorting algorithms, categorized by importance:


🔥 Must-Know Sorting Algorithms (Used Frequently in Interviews)

1️⃣ Quick Sort (Divide and Conquer)

2️⃣ Merge Sort (Divide and Conquer)

3️⃣ Heap Sort (Priority Queue-Based Sorting)


Important to Know (Less Common in Interviews but Useful)

4️⃣ Counting Sort (Non-Comparison Based)

5️⃣ Radix Sort (Non-Comparison Based)


🎯 Basic Sorting Algorithms (Not Used in Interviews but Good for Understanding Basics)

6️⃣ Bubble Sort

7️⃣ Selection Sort

8️⃣ Insertion Sort


📌 Which Sorting Algorithms Should You Master for Interviews?

  1. Quick Sort 🏆 (Most commonly asked in interviews)
  2. Merge Sort 💡 (Guaranteed O(n log n), useful in recursion-based problems)
  3. Heap Sort ⚙️ (Efficient in-place sorting)
  4. Counting Sort & Radix Sort 🚀 (For non-comparison sorting problems)

JavaScript and sorting algo

In JavaScript (specifically in V8, which powers Node.js and Chrome), the built-in .sort() method uses different sorting algorithms based on the data type and length of the array:

  1. TimSort (a hybrid of Merge Sort and Insertion Sort)
    • Used for arrays of numbers and strings in most cases.
    • TimSort is optimized for partially sorted data, making it efficient in real-world scenarios.
  2. Insertion Sort
    • Used for small arrays (length ≤ 10) because it has low overhead and is fast for small datasets.

How it works in V8:

Time Complexity:


Learning

Recursive and D&C