I Implemented Every Sorting Algorithm in Python — And Python's Built-in Sort Crushed Them All

Published: (February 5, 2026 at 01:13 AM EST)
2 min read
Source: Dev.to

Source: Dev.to

Benchmark Results

Algorithm100 elements1,000 elements5,000 elementsPractical Limit
Bubble Sort0.001 s0.15 s3.2 s~500 elements
Selection Sort0.001 s0.13 s2.8 s~500 elements
Insertion Sort0.0005 s0.08 s1.9 s~1,000 (great on nearly‑sorted!)
Merge Sort0.002 s0.025 s0.14 sUsable but slow
Quick Sort0.002 s0.021 s0.11 sUsable but recursion hurts
Heap Sort0.002 s0.029 s0.16 sReliable but never wins
sorted() (Timsort)0.0003 s0.0045 s0.025 sUse this always

Key Takeaways

  • Insertion sort beats Merge/Quick on very small inputs; Python’s function calls are costly: they involve method dispatch and type checks, not a single CPU instruction.
  • Recursion overhead makes Quick sort’s function calls expensive.
  • Memory allocations: Merge sort creates thousands of temporary lists, leading to GC pauses.
  • Timsort is a hybrid genius: it detects runs, uses insertion sort for small chunks, and merges adaptively, all in optimized C.

Insertion Sort Example

def insertion_sort(arr):
    """Return a sorted copy of *arr* using insertion sort."""
    arr = arr.copy()
    for i in range(1, len(arr)):
        key = arr[i]
        j = i - 1
        while j >= 0 and arr[j] > key:
            arr[j + 1] = arr[j]
            j -= 1
        arr[j + 1] = key
    return arr

Never implement your own sort in production Python.
Use sorted() or list.sort() — they’re faster, stable, and battle‑tested. Write your own only for learning purposes or rare edge cases.

Further Resources

  • Detailed explanations – (link to the full article)
  • All source code – (link to the repository)
  • Benchmark script – (link to the script)
  • Raw benchmark data – (link to the data)
  • Full blog post:
  • GitHub repository:

What surprised you most about Python’s sorting performance?
Feel free to share your thoughts in the comments.

Back to Blog

Related posts

Read more »

Problem 10: Duplicate Removal

Problem Description We need a function that removes duplicates from a list while preserving the original order of elements. Example remove_duplicates1, 2, 2, 3...