Sorting Algorithms

Comparison of fundamental sorting algorithms with time and space complexity.

  • JSON6,110 B
  • YAML3,887 B · −36%
  • TOON2,661 B · −56%

Sorting algorithms with best, average and worst time, space, stability and notes: bubble, selection, insertion, merge, quick, heap, counting and radix sort, extended with Timsort, introsort, bucket sort and shell sort.

Use it when

  • You need to choose a sort or explain the trade-offs among them.
  • You need to know whether a sort is stable or in place.

Watch out for

  • Counting, radix and bucket sort assume a bounded key range or distribution.
  • Standard libraries use hybrid algorithms; textbook sorts are mainly for understanding.

The data

NameBestAverageWorstSpaceStableNotes
Bubble SortO(n)O(n^2)O(n^2)O(1)trueSimple but inefficient for large inputs.
Selection SortO(n^2)O(n^2)O(n^2)O(1)falseMinimizes swaps; good when write cost is high.
Insertion SortO(n)O(n^2)O(n^2)O(1)trueEfficient for small or nearly sorted datasets.
Merge SortO(n log n)O(n log n)O(n log n)O(n)trueConsistent performance; good for linked lists.
Quick SortO(n log n)O(n log n)O(n^2)O(log n)falseIn-place; widely used in practice.
Heap SortO(n log n)O(n log n)O(n log n)O(1)falseGuaranteed O(n log n); in-place.
Counting SortO(n + k)O(n + k)O(n + k)O(k)trueOnly for integers in bounded range.
Radix SortO(nk)O(nk)O(nk)O(n + k)trueSorts by digit position; k = number of digits.
TimsortO(n)O(n log n)O(n log n)O(n)trueHybrid of merge and insertion sort that exploits existing runs; used in Python and Java for objects.
IntrosortO(n log n)O(n log n)O(n log n)O(log n)falseStarts as quicksort and falls back to heapsort when recursion gets too deep; common in C++ std::sort.
Bucket SortO(n + k)O(n + k)O(n^2)O(n + k)trueDistributes items into buckets and sorts each; good for uniformly distributed input.
Shell SortO(n log n)Depends on gap sequenceO(n^2) for simple gapsO(1)falseGeneralizes insertion sort with diminishing gaps; simple and in place.

Fields

The shape of this dataset, read from the data itself: every column, its type and a real value. Agents can use this to plan a query without fetching the whole document.

SectionFieldTypeExample
datanamestringBubble Sort
databeststringO(n)
dataaveragestringO(n^2)
dataworststringO(n^2)
dataspacestringO(1)
datastablebooleantrue
datanotesstringSimple but inefficient for large inputs.

Fetch the same bytes

The static files contain the same reference rows as the API, with no API rate limit. Live metadata, such as view counts, may differ. Use the API for content negotiation; use the static files for a cacheable snapshot.

curl "https://yjtoon.com/api/dataset/sorting-algorithms?format=toon"
const res = await fetch(
  "https://yjtoon.com/static-data/dataset/sorting-algorithms.toon"
);
const toon = await res.text();

Rate limit: 120 requests per minute per IP, no key and no signup. API reference →

Topics

  • algorithms
  • sorting
  • complexity
  • big-o

Search the library

Search by a topic, title, or keyword.

Start with a collection

Browse all 205 datasets