Sorting Summary - aloalgo.com

Sorting Summary

With so many sorting algorithms to choose from, how do you know which one to use? This guide focuses on the practical question: given a situation, which sorting algorithm should you reach for?

Quick Reference

Here's your cheat sheet for choosing a sorting algorithm:
SituationBest ChoiceWhy
General purposeQuickSortFastest average case, in-place
Need stabilityMergeSortPreserves equal element order
Nearly sorted dataInsertion SortO(n) for nearly sorted
Linked listsMergeSortNo random access needed
Small integers (0-k)Counting SortO(n+k) linear time
Need kth element onlyQuickSelectO(n) average, no full sort
Memory constrainedHeapSortO(1) extra space, guaranteed O(n log n)

Complexity Comparison

AlgorithmBestAverageWorstSpaceStable
QuickSortO(n log n)O(n log n)O(n²)O(log n)No
MergeSortO(n log n)O(n log n)O(n log n)O(n)Yes
HeapSortO(n log n)O(n log n)O(n log n)O(1)No
Insertion SortO(n)O(n²)O(n²)O(1)Yes
Selection SortO(n²)O(n²)O(n²)O(1)No
Bubble SortO(n)O(n²)O(n²)O(1)Yes
Counting SortO(n+k)O(n+k)O(n+k)O(k)Yes
Radix SortO(dn)O(dn)O(dn)O(n+k)Yes

Chapters

Learn each sorting algorithm in detail:

Interview Tips

  • Know the trade-offs: Time vs space, stability, worst case guarantees
  • Python's sorted(): Uses Timsort (hybrid of merge + insertion sort), O(n log n), stable
  • Partial sorting: Use QuickSelect for kth element, heaps for top-k
  • Custom comparators: Know how to sort by custom keys
  • In-place requirement: QuickSort or HeapSort, not MergeSort
  • Linked list sorting: Always MergeSort (no random access)

Practice Problems

Sorting ApplicationsPartition & SelectionSorted Array Problems

Final Thoughts

Sorting is less about memorizing algorithms and more about understanding trade-offs. In interviews, the key is quickly identifying which properties matter for your specific problem: Do you need stability? Is space constrained? Is the data nearly sorted? Is the range of values limited? Answer these questions, and the right algorithm becomes obvious.
Was this helpful?
© 2026 aloalgo.com. All rights reserved.