Algoramic

Entry point


Subjects

  • Overview
Privacy Policy·© Algoramic
HomeSorting Algorithms

Sorting Algorithms

Build intuition for how and why different sorts work, and how their cost grows with input size.

Elementary Sorts

Simple comparison sorts — easy to follow, but O(n²) once the input grows.

Bubble Sort

Watch bubble sort compare neighbouring values and swap them, one step at a time, until the largest bubble to the end.

Selection Sort

Scan the unsorted part for the smallest value and move it to the front — a growing sorted region, one selection at a time.

Insertion Sort

Build a sorted list one item at a time — each new value sinks left into its place, like sorting cards in your hand.

Shell Sort

Insertion sort with a head start: swap elements a large gap apart, then shrink the gap so values leap most of the way home. In-place; speed depends on the gap sequence.

Efficient Sorts

Comparison sorts that reach the O(n log n) speed limit.

Merge Sort

Divide and conquer: split the array into halves down to single elements, then merge the sorted pieces back together — reliably O(n log n).

Quicksort

Divide and conquer by partitioning: pick a pivot, put smaller values left and larger right, then sort each side. Fast in practice — average O(n log n), in-place.

Heapsort

Build a max-heap inside the array, then repeatedly extract the largest to the back — reliably O(n log n) and in-place.

Hybrid Sorts

Real-world defaults that combine several strategies to be fast and safe everywhere.

Timsort & Introsort: Hybrid Sorts

The sorts your language actually ships: Timsort and introsort combine insertion, merge, quicksort and heapsort to be fast in practice and O(n log n) in the worst case.

Non-Comparison Sorts

Sort by the values themselves — beating the comparison limit on the right kind of data.

Counting Sort

A non-comparison sort: drop each value into a bin for its value, then read the bins in order. O(n + k) when the value range k is small.

Radix Sort

Sort multi-digit numbers without comparisons: bucket by one digit at a time, least significant first, using a stable counting pass each round. O(d·(n + k)).

Bucket Sort

Scatter values into range buckets, sort each bucket, then gather in order. O(n) average when the data is spread evenly; O(n²) worst case.

Comparisons & Properties

How the sorts stack up against each other — cost by input size, and stability.

Bubble Sort vs Quicksort: Cost by Input Size

Two sorts, one set of axes. Slide the input size n and watch O(n²) pull away from O(n log n) — the same comparison made concrete, with live counts.

Stable vs Unstable Sorting

A sort is “stable” when items with equal keys keep their original order. See the difference on a tie, and why it matters for sorting by more than one key.

Related categories

Algorithmic ComplexityFoundations