How Does a Computer Sort a List? | How It Works
On this page
What is the short answer?
A computer sorts a list by repeating one small job. It compares two items and, if they are in the wrong order, swaps or moves them. A sorting algorithm is the plan that decides which pairs to compare, and when.
Simple plans, such as bubble sort, selection sort and insertion sort, do work that grows roughly with the square of the list's length on a typical list. Merge sort and quicksort split the list into parts, which cuts the work to about n log n steps. No method that learns the order only by comparing can guarantee much better.
Definition: Sorting is rearranging items so that their keys are in ascending order, as Princeton University's algorithms course text puts it. The key is the value the order follows, such as a mark or a name.
Why does a computer need a sorted list?
Princeton University's Algorithms booksite, by Robert Sedgewick and Kevin Wayne, in the United States, gives one reason. Sorted data can be searched efficiently with binary search.
Binary search compares the wanted item with the middle item. If the wanted item is smaller, the search continues in the left half; if larger, in the right half. Each comparison discards about half of what is left.
The booksite says binary search in a sorted list of N keys uses no more than lg N + 1 compares. Here lg is the logarithm to base 2, roughly the number of times N can be halved before reaching 1.
For an illustrative 1,000,000 names, lg N + 1 is about 20.9, so at most 20 compares. Checking an unsorted list name by name could take 1,000,000.
How do simple sorts compare and swap?
On the illustrative list [5, 2, 4, 1], each of the three simplest sorts makes 6 comparisons. They differ in what follows each comparison.
- Bubble sort. The Dictionary of Algorithms and Data Structures of NIST, the United States' National Institute of Standards and Technology, defines it as comparing each adjacent pair in turn, swapping if needed, and repeating passes until a pass makes no swaps.
- Selection sort. Each pass finds the smallest unsorted item and swaps it to the front. Here the first pass makes 3 comparisons and swaps 1 with 5, giving [1, 2, 4, 5]; two more passes, of 2 and 1 comparisons, change nothing.
- Insertion sort. Each item slides left past larger ones, like a card slotted into a sorted hand. Here 2, 4 and 1 take 1, 2 and 3 comparisons, with 5 swaps in all.
Princeton's count gives selection sort about n²/2 compares and n exchanges for n items, some of an item with itself. Insertion sort averages about n²/4 of each, with about n²/2 in the worst case, but only n − 1 compares on a list already sorted.
How do merge sort and quicksort split the work?
Both are divide-and-conquer methods: they turn one big sorting job into smaller ones.
Merge sort, in Princeton's description, divides the list into two halves, sorts each half the same way, then merges them: compare the two front items, output the smaller, repeat.
On [5, 2, 4, 1], the halves become [2, 5] and [1, 4] after 1 comparison each. Merging compares 2 with 1, 2 with 4 and 5 with 4, then 5 is left alone. That is 5 comparisons in all. The price, Princeton notes, is extra space in proportion to n.
Quicksort splits before sorting. NIST describes picking one item, the pivot, and dividing the rest into smaller and larger items; Princeton calls this partitioning, then sorts each part the same way.
In a simple version on [5, 2, 4, 1], with 5 as pivot, three comparisons put 2, 4 and 1 on its left. Then pivot 2 sends 1 left and 4 right, in two comparisons. Again 5 comparisons, with no merging.
Where did quicksort come from?
Quicksort's inventor is Tony Hoare (C. A. R. Hoare), but accounts give different years.
- University of Oxford, United Kingdom. Its Department of Computer Science says that in 1959, as a graduate student of machine translation at Moscow State University, Hoare discovered Quicksort to look up dictionary words efficiently. He returned to England in 1960, programming for Elliott Brothers, and moved to Oxford in 1977.
- Computer History Museum. A tribute by Dag Spicer, dated 11 March 2026, also places Quicksort in 1959 at Moscow State University. Hoare died on 5 March 2026, aged 92.
- Princeton University, United States. Its course text says Hoare invented quicksort in 1960.
Hoare's paper "Quicksort", written at Elliott Brothers, appeared in The Computer Journal in 1962, describing a new method of sorting in a computer's random-access store.
How do computer scientists count the steps a sort takes?
Rather than timing one machine, Princeton's text counts compares and exchanges, then describes the count by its growth rate: how fast it rises as the list lengthens.
NIST defines the usual tool, big-O notation. Writing f(n) = O(g(n)) means that beyond some size, f(n) never exceeds a fixed multiple of g(n). Constant factors drop out, leaving the shape of the growth.
Bubble sort without early stopping makes n − 1 comparisons, then n − 2, down to 1: n(n − 1) ÷ 2 in total. For 1,000 items that is 499,500, close to n²/2. The count is O(n²), so doubling the list roughly quadruples the work.
Merge sort can halve a list about lg n times, and each level's merges handle all n items once. Princeton puts it between ½ n lg n and n lg n compares. Quicksort averages about 2n ln n, or about 1.39 n lg n, but needs about n²/2 in the worst case, so Princeton's version shuffles the list first.
India's NPTEL course archive lists Madhavan Mukund's Design and Analysis of Algorithms, from the Chennai Mathematical Institute, which ran from 23 January to 17 March 2017. Its outline lists O() notation, and its second week has five modules on sorting, from insertion sort to stability.
Why can no comparison sort beat about n log n?
Princeton's text states the limit: no compare-based algorithm can guarantee to sort n items with fewer than lg(n!) compares, about n lg n. Here n!, "n factorial", is 1 × 2 × … × n, the number of orders n distinct items can arrive in.
The reason is counting. Each comparison has two answers, so k comparisons give at most 2 to the power k answer patterns. A sort must separate all n! orders, since each needs a different rearrangement.
For 4 items there are 24 orders. Four comparisons separate at most 2⁴ = 16; five separate 32. So some list of 4 needs at least 5 comparisons, and merge sort, never needing more than 5 here, meets the limit.
The limit binds only methods that compare. MIT's course 6.006, in Erik Demaine's autumn 2011 lecture, uses the comparison model to prove lower bounds for sorting, then turns to counting sort and radix sort, which run in linear time. NIST says counting sort counts each key's occurrences, then puts each item in its final place, and is efficient when distinct keys are few compared with the number of items.
Does the starting order matter, and what makes a sort stable?
For some sorts it matters a great deal. NIST gives bubble sort O(n²) for arbitrary data but time approaching linear on a nearly ordered list, since a pass without swaps ends it. Princeton says insertion sort works well for certain non-random lists common in practice; on the already sorted [1, 2, 4, 5] it makes 3 comparisons and no swaps.
Princeton defines a stable sort as one that keeps equal keys in their original relative order. Insertion sort and merge sort are stable; selection sort, shellsort, quicksort and heapsort are not.
Stability lets a list be sorted twice: a class list in name order, sorted stably by mark, keeps equal marks in name order. In the illustrative list [2a, 2b, 1], with two equal keys tagged a and b, selection sort's first swap gives [1, 2b, 2a], reversing them. Insertion sort gives [1, 2a, 2b].
How does this work with real numbers?
Both examples use illustrative inputs.
How does bubble sort put [5, 2, 4, 1] in order?
Pseudocode: bubble sort
- Make passes 1 to n − 1.
- In pass p, compare neighbours from position 1 to n − p; swap if the left is larger.
- Stop early if a pass makes no swaps.
With n = 4 there are at most 3 passes. Each pass carries the largest remaining value to the end.
Table: Bubble sort on the illustrative list [5, 2, 4, 1]
| Pass | Comparisons | Swaps | List after |
|---|---|---|---|
| 1 | 5 with 2, 5 with 4, 5 with 1 | 3 | [2, 4, 1, 5] |
| 2 | 2 with 4, 4 with 1 | 1 | [2, 1, 4, 5] |
| 3 | 2 with 1 | 1 | [1, 2, 4, 5] |
Comparisons: 3 + 2 + 1 = 6, matching n(n − 1) ÷ 2 = 6. Swaps: 5, one for each inversion, a pair in the wrong order: 5 and 2, 5 and 4, 5 and 1, 2 and 1, 4 and 1.
How fast do n² and n lg n grow apart?
Rounded, lg 1,000 is 9.97, and lg 1,000,000 is 19.93.
Table: Rough step counts for illustrative list sizes
| Items (n) | About n² | About n lg n | Ratio |
|---|---|---|---|
| 1,000 | 1,000,000 | 1,000 × 9.97 ≈ 10,000 | about 100 |
| 1,000,000 | 1,000,000,000,000 | 1,000,000 × 19.93 ≈ 20,000,000 | about 50,000 |
At an illustrative 1,000,000,000 steps a second, 1,000,000,000,000 steps take 1,000 seconds, about 17 minutes; 20,000,000 steps take 0.02 seconds.
Where can the student go deeper in World 101?
The World 101 subject Algorithms & Computation covers the family sorting belongs to: exact, finite procedures that turn an input into a wanted output, and how their cost is measured. Its related ideas include randomised algorithms, and Princeton calls its quicksort one, because it shuffles first.
Three related subjects carry these ideas further. Computing Fundamentals looks at the machines that carry out compares and swaps, and at how a list sits in memory. Logic, Proof & Verification deals with proofs like the n log n limit, and with checking that a procedure always gives the right answer. Big Data & Data Science works with millions of records, where the gap between n² and n lg n decides how long a job takes.
Sources
- R. Sedgewick and K. Wayne, Algorithms, 4th Edition booksite, Princeton University: "Elementary Sorts", modified 3 May 2021; "Mergesort", 20 March 2021; "Quicksort", 9 March 2022 (Hoare, 1960); "Sorting Applications", 18 March 2018; "Elementary Symbol Tables", 1 February 2017.
- P. E. Black, ed., Dictionary of Algorithms and Data Structures, NIST: "bubble sort", 7 April 2023; "merge sort" and "quicksort", 6 April 2023; "counting sort", 21 April 2022; "big-O notation", 6 September 2019.
- MIT OpenCourseWare, 6.006 Introduction to Algorithms, Fall 2011, Lecture 7 (E. Demaine), read 18 November 2026.
- University of Oxford, Department of Computer Science, Tony Hoare profile (1959, 1960, 1977), read 18 November 2026.
- D. Spicer, "In Memoriam: Sir Antony Hoare (1934 to 2026)", Computer History Museum, 11 March 2026.
- C. A. R. Hoare, "Quicksort", The Computer Journal 5(1), 1962.
- NPTEL archive, "Design and Analysis of Algorithms", M. Mukund, Chennai Mathematical Institute, 2017, read 18 November 2026.
Common errors and misconceptions
- Misconception: A fast enough computer makes the choice of sort unimportant. Correct: Speed shortens every step, but growth rates differ; for 1,000,000 items, about n² steps is roughly 50,000 times n lg n.
- Misconception: A clever comparison sort could beat n log n comparisons on every list. Correct: Princeton's text states no compare-based sort can guarantee fewer than lg(n!), about n lg n.
- Misconception: Quicksort always takes about n log n steps. Correct: Its worst case is about n²/2 compares, which random shuffling guards against; NIST calls it typically O(n log n).
- Misconception: Big-O notation gives the exact number of steps. Correct: NIST defines it as a bound up to a fixed multiple, so n(n − 1) ÷ 2 is O(n²).
- Misconception: Every sort keeps equal keys in their original order. Correct: Only stable sorts do; Princeton lists selection sort and quicksort as not stable.
Key takeaways
- A computer sorts by comparing two items at a time and swapping or moving them; the algorithm decides which pairs to compare and when.
- Sorted data allows binary search, which Princeton's text says needs no more than lg N + 1 compares, so at most 20 for 1,000,000 items.
- Bubble, selection and insertion sort need about n²/2 comparisons in the worst case, so doubling a list roughly quadruples their work.
- Merge sort splits, sorts and merges in at most n lg n compares; quicksort partitions around a pivot and averages about 1.39 n lg n.
- No compare-based sort can guarantee fewer than lg(n!) compares, about n lg n, because each comparison has two answers and n! orders must be separated.
- Oxford and the Computer History Museum date Hoare's quicksort to 1959, Princeton to 1960; his paper appeared in 1962.
Test yourself
How many comparisons and swaps does bubble sort make on [5, 2, 4, 1]?
Six comparisons (3 + 2 + 1) and 5 swaps, one for each pair in the wrong order.
Why does binary search need a sorted list?
It compares with the middle item and discards the half that cannot hold the wanted item, which works only if the order is known.
How does merge sort sort [5, 2, 4, 1] in 5 comparisons?
One comparison sorts each half into [2, 5] and [1, 4]; merging them takes three more.
Why must a comparison sort make at least 5 comparisons on some list of 4 items?
There are 24 possible orders, and 4 two-way comparisons separate at most 16 cases; 5 separate 32.
What makes a sort stable?
It keeps items with equal keys in their original relative order, as insertion sort and merge sort do.
About how many steps do n² and n lg n give for 1,000,000 items?
About 1,000,000,000,000 against about 20,000,000, rough counts about 50,000 times apart.
Curious Computing: how computers, code and the internet work.
Go deeper in World 101: Algorithms & Computation
Write about it
Turn a question from this chapter into a paper.
The Writing Studio has a structure guide, a rubric and citation tools for 15 formats, from essays to research papers. A free account keeps up to five drafts. Learnacy Essential adds feedback from an AI writing coach and submission to The OYI Review.
Publication depends on review and is not guaranteed.
