Notes

Search

Search IconIcon to open search

Sorting

Last updated Jan 1, 2022

Sorting

Informally: Given items, put them in order. This is useful because can do things like:

Knuth’s Definition

An ordering relation < for keys a, b, and c is has the following properties:

An ordering relation with the properties above is also known as a “total order”.

A sort is a permutation of a sequence of elements that puts the keys into non-decreasing order relative to a given ordering relation.

Inversions

We can have an alternate perspective of sorting. An inversion is a pair of elements that are out of order with respect to <.

For example, in the sequence $[1, 3, 2, 4]$, the pair $(3, 2)$ is an inversion.

Another way to think about sorting: