Skip to content

Instantly share code, notes, and snippets.

@KPiyush24
Created November 1, 2025 12:01
Show Gist options
  • Save KPiyush24/4d92a0d2ac16bf850150c1bb13e0062c to your computer and use it in GitHub Desktop.
Save KPiyush24/4d92a0d2ac16bf850150c1bb13e0062c to your computer and use it in GitHub Desktop.
Sortings

Sorting Algorithms Comparison Table

Quick Reference Table

Algorithm Best Average Worst Space Stable
Bubble Sort O(n) O(n²) O(n²) O(1) Yes
Selection Sort O(n²) O(n²) O(n²) O(1) No
Insertion Sort O(n) O(n²) O(n²) O(1) Yes
Merge Sort O(n log n) O(n log n) O(n log n) O(n) Yes
Quick Sort O(n log n) O(n log n) O(n²) O(log n) No
Heap Sort O(n log n) O(n log n) O(n log n) O(1) No
Counting Sort O(n + k) O(n + k) O(n + k) O(k) Yes
Radix Sort O(d(n + k)) O(d(n + k)) O(d(n + k)) O(n + k) Yes
Bucket Sort O(n + k) O(n + k) O(n²) O(n + k) Yes
Shell Sort O(n log n) O(n^1.5) O(n²) O(1) No
Tim Sort O(n) O(n log n) O(n log n) O(n) Yes

Note: k = range of input values, d = number of digits


Detailed Comparison

Algorithm How It Works When to Use Best Use Cases
Bubble Sort Repeatedly compares adjacent elements and swaps them if wrong order. Largest element "bubbles" to end each pass. Educational purposes, tiny arrays Teaching concepts, checking if already sorted
Selection Sort Finds minimum from unsorted part, swaps with first unsorted element. Builds sorted region from left. Small arrays, minimize swaps Systems where write operations are expensive (only n-1 swaps)
Insertion Sort Takes each element and inserts into correct position in sorted portion. Like sorting cards in hand. Small/nearly sorted data Arrays < 50 elements, nearly sorted data, part of Timsort/Introsort
Merge Sort Divide array in half recursively, sort halves, merge sorted halves together. Large data, need stability Guaranteed O(n log n), linked lists, external sorting, parallel processing
Quick Sort Pick pivot, partition around it (smaller left, larger right), recursively sort partitions. General purpose sorting Default choice for arrays, cache-friendly, average case excellent
Heap Sort Build max heap, repeatedly extract max and rebuild heap with remaining elements. Need O(n log n) + O(1) space Space-constrained systems, real-time systems, priority queues
Counting Sort Count occurrences of each value, use counts to place elements in sorted order. Integer sorting, small range Sorting integers 0-100, when k ≈ n, as subroutine in Radix Sort
Radix Sort Sort digit by digit from least to most significant, using stable sort for each digit. Fixed-length integers/strings Sorting IDs, phone numbers, fixed-length strings, multiple keys
Bucket Sort Distribute elements into buckets, sort buckets individually, concatenate results. Uniformly distributed data Floating points in [0,1), uniformly distributed data
Shell Sort Generalized insertion sort with gap sequences. Sorts elements far apart first, reduces gap. Medium-sized arrays Better than insertion for medium data, embedded systems (simple code)
Tim Sort Hybrid of Merge + Insertion. Identifies runs, uses insertion for small runs, merges runs. Real-world data with patterns Python/Java default, real-world data (often partially sorted)

Comparison by Category

Best for Small Data (< 50 elements)

  • Insertion Sort
  • Selection Sort (if writes are expensive)

Best for Large Data

  • Quick Sort (average case)
  • Merge Sort (guaranteed performance)
  • Heap Sort (space-constrained)

Best for Nearly Sorted Data

  • Insertion Sort - O(n)
  • Tim Sort - O(n)
  • Bubble Sort - O(n) with optimization

Best for Specific Data Types

  • Integers with small range: Counting Sort
  • Fixed-length integers: Radix Sort
  • Uniformly distributed: Bucket Sort
  • Linked Lists: Merge Sort

Stable Sorts (preserve order of equal elements)

  • Bubble Sort
  • Insertion Sort
  • Merge Sort
  • Counting Sort
  • Radix Sort
  • Bucket Sort
  • Tim Sort

In-Place Sorts (O(1) or O(log n) space)

  • Bubble Sort
  • Selection Sort
  • Insertion Sort
  • Quick Sort
  • Heap Sort
  • Shell Sort

Real-World Usage Examples

Scenario Best Algorithm Reason
Sorting user IDs (0-1000000) Counting Sort Small range integers
Sorting names alphabetically Tim Sort / Merge Sort Need stability, string comparison
Embedded system (limited memory) Heap Sort O(1) space, guaranteed O(n log n)
Database query results Merge Sort External sorting capability
Game leaderboard (small) Insertion Sort Small, frequently updated data
Library sort function (Java/Python) Tim Sort Excellent for real-world data patterns
QuickSelect for kth element Quick Sort partitioning Average O(n) selection
Sorting 1 million phone numbers Radix Sort Fixed-length numeric strings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment