Math 2.0

Computer science Definition

Sorting algorithm

Builds on AlgorithmTotal orderPermutation

Definition

Let (X,≤)(X, \le) be a totally ordered set. A sorting algorithm for XX is an algorithm whose inputs are finite sequences (a1,…,an)(a_1, \dots, a_n) over XX. On each input it outputs a sequence (aσ(1),…,aσ(n))(a_{\sigma(1)}, \dots, a_{\sigma(n)}) such that:

  1. σ\sigma is a permutation of {1,…,n}\set{1, \dots, n};
  2. aσ(1)≤aσ(2)≤⋯≤aσ(n)a_{\sigma(1)} \le a_{\sigma(2)} \le \cdots \le a_{\sigma(n)}.

The algorithm is stable if moreover σ(i)<σ(j)\sigma(i) < \sigma(j) whenever i<ji < j and aσ(i)=aσ(j)a_{\sigma(i)} = a_{\sigma(j)}. It is a comparison sort if it accesses the elements only through tests of the form ai≤aja_i \le a_j.

Remarks

In practice one often sorts records by a key, and ≤\le is then a total preorder on records. Stability matters only in that setting, or when equal elements can be told apart.

Example: on input (3,1,2)(3, 1, 2), the output (1,2,3)(1, 2, 3) is correct, with σ=(2,3,1)\sigma = (2, 3, 1) in one-line notation, so σ(1)=2\sigma(1) = 2, σ(2)=3\sigma(2) = 3, σ(3)=1\sigma(3) = 1.

Non-example: an algorithm that outputs (1,1,2)(1, 1, 2) on (2,1,1,3)(2, 1, 1, 3) fails clause 1, since its output is not a rearrangement of all four input elements. An algorithm that outputs its input unchanged fails clause 2 on (2,1)(2, 1).

Reading path

To understand this page from the ground up, read these 4 pages in order. The foundations come first.

Show the path as a graph
Each page uses the pages above it. The colour of the bar on a box shows how strong the evidence is, and the box also gives it as text. The list has the same pages.
  • no evidence level
  1. Algorithm concept
  2. Permutation concept
  3. Total order concept
  4. Sorting algorithm definition

Further reading

  1. Web https://en.wikipedia.org/wiki/Sorting_algorithm
About this page
TagB6ADA6RGAM
URIm2://B6ADA6RGAM
Kinddefinition
WritingW0 stub
MSC68P10, 68W40
DefinesSorting algorithm
UsesAlgorithm, Total order, Permutation
Changed3 November 2026

Markdown · JSON · Edit on GitHub

What this page needs for W1

  • a source with a locator (0 of 1)

The writing levels say how complete the text of a page is. They do not measure its truth.

What links here

No page links to this page yet.