BeginnerTopic 18 of 104Software & Computing

Sorting algorithms

Bubble, insertion, merge and quick sort have different complexity trade-offs.

In depth

Bubble, insertion and selection sorts are O(n²) but simple; merge sort and heap sort are O(n log n); quicksort is average O(n log n) with in-place partitioning; counting and radix sorts are linear for bounded keys. Stability and memory use matter. Libraries use hybrids like Timsort.

Key points

  • Comparison sort lower bound n log n
  • Stable vs unstable
  • In-place
Example

Python's sorted() uses Timsort and runs in O(n log n).

Why it matters

Sorting is used everywhere, and the choice of sorting algorithm depends on data size, memory and whether the sort must be stable.

Where this is used

  • Ordering a list of test results by date.
  • Sorting a large database extract that does not fit in memory.
  • Choosing a sort for nearly sorted sensor logs.

Frequently asked questions

What is Sorting algorithms?

Bubble, insertion, merge and quick sort have different complexity trade-offs.

Why is it important to understand sorting algorithms?

Sorting is used everywhere, and the choice of sorting algorithm depends on data size, memory and whether the sort must be stable.

Which sort is fastest in practice?

Quick sort is often fastest on average for in-memory data, while merge sort gives guaranteed time and is stable.

What is a stable sort?

A sort that keeps the original order of equal elements, which matters when sorting by several keys in turn.

How is Sorting algorithms used in practice?

Typical applications include ordering a list of test results by date, sorting a large database extract that does not fit in memory and choosing a sort for nearly sorted sensor logs.

What should I learn before Sorting algorithms?

Study "Linear and binary search" first (topic 17 of 104 in Software & Computing). It covers the groundwork this topic builds on.

What are the key points of Sorting algorithms?

Comparison sort lower bound n log n. Stable vs unstable. In-place.

Can you give an example of Sorting algorithms?

Python's sorted() uses Timsort and runs in O(n log n).

What comes after Sorting algorithms?

The next topic is "Stacks and queues", which builds directly on this one.

How difficult is Sorting algorithms?

It is classed as Beginner in Software & Computing (topic 18 of 104). It is an entry-level topic. It relies only on basic school mathematics and the ideas introduced earlier in this discipline.

Learning path

Want to practise? Open the interactive course for quick checks, quizzes, simulations and progress tracking.

Related topics