Skip to content

sorting algorithm

A sorting algorithm is an algorithm that arranges the elements of a sequence into a defined order, most often ascending or descending by a key. Sorting is one of the most studied problems in computing, because ordered data makes many later operations faster and simpler.

Sorting algorithms differ in speed, memory use, and behavior on already-ordered input. Comparison-based methods like quicksort, merge sort, and heapsort decide order by comparing pairs of elements, and they cannot beat O(n log n) time in the worst case. Non-comparison methods like counting sort and radix sort instead exploit the structure of the keys to run faster on suitable data.

These families split the common algorithms two ways:

Sorting algorithms split into Comparison based (quicksort, merge sort, heapsort) and Non comparison (counting sort, radix sort).
Two Families: Compare Pairs or Exploit Keys

Two further properties matter in practice. A stable sort preserves the relative order of equal elements, and an in-place sort rearranges the data without allocating a second copy. Ordered output also enables fast lookup through binary search.

Most languages ship a well-tuned general-purpose sort algorithm, so hand-written sorting is rare in application code. Python’s built-in sort uses Timsort, a hybrid of merge sort and insertion sort. Knowing the underlying algorithm still helps in predicting time complexity.

Tutorial

Sorting Algorithms in Python

In this tutorial, you'll learn all about five different sorting algorithms in Python from both a theoretical and a practical standpoint. You'll also learn several related and important concepts, including Big O notation and recursion.

intermediate algorithms python

For additional information on related topics, take a look at the following resources:


By Martin Breuss • Updated June 22, 2026 • Reviewed by Leodanis Pozo Ramos