Sortedness#
Before we introduce our next algorithm, we need to take a brief detour to talk about sortedness.
We say a list is sorted when each element is no smaller than the previous one. Let’s unpack that definition:
If our list is
[x, y, z]
, then it must be thatx <= y
andy <= z
(sox <= z
transitively).By default, sortedness means sorted ascending.
The empty list is trivially sorted, e.g.,
[]
is sorted.A list with one element is trivially sorted, e.g.,
[47]
is sorted.Sorted lists can have duplicates, e.g.,
[0,1,1,2,2,2]
is sorted ascending.
People sometimes refer to other notions of sorting, like sorted descending or sorted on a particular aspect (e.g., sorting by last name and then first name). But if nothing else is specified, the default is to sort ascending.