Virtual laboratory/Mathematics/Graph theory/Sorting algorithms — complexity

Sorting algorithms — complexity

An extension of the topic of computational complexity and functions (beyond the curriculum — computer science). Simple bubble sort performs ~n² comparisons, while a clever one (merge sort) performs ~n·log n — for large n the difference is enormous (for n=1000 that is a hundred times fewer operations). Merge sort was devised by John von Neumann in 1945.

Sorting algorithms — complexity
Mathematics · Graph theory · Sorting algorithms — complexity (1945)
Number of elements to sort nn20 items
An efficient algorithm sorts n elements in time of order…
elements n
20
bubble ≈ n²
400
merge ≈ n·log n
86
times fewer
×5
🎯 Challenge: classify the complexity
streak: 0

I’ll give you a sorting algorithm — assign it a complexity class. Simple algorithms are O(n²), clever “divide and conquer” ones are O(n·log n).

What just happened?

Sorting — arranging data in ascending or descending order — is one of the most common operations in computer science, and performance is decided by the choice of algorithm. We raised the number of elements to 1,000 and compared two strategies: simple bubble sort and clever merge sort.

Computational complexity (big-O notation)

Complexity describes how the number of operations grows with the data size nn. Simple algorithms (bubble sort, selection sort) compare every element with every other, so they perform on the order of:

O(n2)O(n^2)

Clever algorithms — merge sort (devised by John von Neumann in 1945) or quicksort — thanks to the “divide and conquer” strategy (split the data into halves, sort recursively, merge the sorted parts) achieve:

O(nlog⁡n)O(n\log n)

Why such an enormous difference?

For small n the difference is tiny. But for n = 1000 that is ~1,000,000 operations (n2n^2) versus ~10,000 (nlog⁡nn\log n) — a hundred times fewer. Sorting a million elements with an O(n2)O(n^2) algorithm takes trillions of operations (hours, even days), while O(nlog⁡n)O(n\log n) — a few tens of millions (a fraction of a second). The choice of algorithm decides whether a program runs for seconds or years.

A foundation of theoretical computer science

Complexity analysis lets us predict whether a program can realistically be run, even before we write it. That is why standard libraries (in Python, Java, C++) use algorithms of order nlog⁡nn\log n internally, rather than naive quadratic loops.

Why this matters for your exams:

The topic of complexity itself lies outside the high-school curriculum (it straddles mathematics and computer science), but logarithms and the growth rate of functions certainly are on it. Being able to compare how n2n^2 grows faster than nlog⁡nn\log n is the same intuition you need in problems about functions and sequences.

Remember: the number of operations grows with n according to the algorithm’s complexity — bubble sort ∼n2\sim n^2, merge sort ∼nlog⁡n\sim n\log n — and the difference escalates sharply with the data size. That is why the choice of algorithm, not a faster computer, decides what can be computed.

Illustrative model · NexTutor Virtual Laboratory

Enjoyed the experiment? Share it.

It's free — send it to friends from school.

Theory & formulas

Time complexity

The number of operations as a function of the data size nn is described by big-O notation:

  • bubble sort: O(n2)O(n^2)
  • merge sort: O(nlog⁡n)O(n\log n)

Why such an enormous difference

For n=1000n=1000: n2=106n^2=10^6, while nlog⁡2n≈104n\log_2 n\approx 10^4 — about a hundred times fewer operations.

Divide and conquer

Merge sort splits the data in half, sorts the parts and merges the results — hence the factor log⁡n\log n (the number of levels of division).

Notation: nn the number of elements, O(⋅)O(\cdot) the order of the number of operations, log⁡\log logarithm.

Frequently asked questions

›

In what time does an efficient algorithm sort?

In time of order n·log n (e.g. merge sort), much faster than n² (bubble sort).

›

What is computational complexity?

A description of how the number of operations grows with the data size n (big-O notation).

›

Who devised merge sort?

John von Neumann, in 1945 — a “divide and conquer” algorithm with complexity O(n·log n).

Teaching? Embed this experiment on your site.

Free widget — paste it on your school site, blog or notes. It links to the full version.

Teaching a class or tutoring? Guide students in NexTutor →