← Course home

Python reference · Reference

Sorting

Compare simple and efficient sorting algorithms by work, memory, stability, and input order.

ReferenceAbout 2 min

This page lists the library modules for Sorting. The lesson is Comparison sorts.

This page is the module index for sorting.

This legacy page is the overview map. Start with comparison sorts, then follow merge sort and quicksort and partition. For ranks and non-comparison methods, see the sorting extras overview.

  • sorting/bubble_sort.py — O(n²) worst and average, O(n) best on sorted input, O(1) extra memory. Stable.
  • sorting/selection_sort.py — O(n²) for every input order. Not stable.
  • sorting/insertion_sort.py — O(n²) worst and average, O(n) best. Stable.
  • sorting/merge_sort.py — O(n log n) time, O(n) extra memory. Stable.
  • sorting/quick_sort.py — Lomuto partition: expected O(n log n), worst O(n²), including an all-equal list. quick_sort_three_way handles duplicates in O(n) when every key matches. Not stable.

Industry

A working engineer calls list.sort. Python's sorting howto names Timsort and says it takes advantage of ordering already present in the data. list.sort is guaranteed stable, and the time-complexity documentation lists it as O(n log n) in the worst case. The quadratic sorts in this lesson are the hand derivations of a worse bound.

Implementation and tests Optional reference

Library code 5

  • bitsandbytes.sorting.bubble_sort
  • bitsandbytes.sorting.selection_sort
  • bitsandbytes.sorting.insertion_sort
  • bitsandbytes.sorting.merge_sort

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.