Sorting algorithm

A sorting algorithm is an algorithm for computers to sort a list of data, possibly unsorted. More formally, sorting algorithms find the sorted permutation of any given input, therefore a valid sorting algorithm must return a list that is in either non-decreasing or non-increasing order containing all the same items matching that of the initial input.

The task in sorting a list is deceptively simple, and many instinctive techniques one uses in their everyday life, such as Selection sort or Insertion sort, prove inefficient in managing large amounts of data often present in digital databases today. As a result, a large part of computer science, in the 1950's, is dedicated to finding solutions to sort efficiently.

Stability
Main page: Stability

A sort is said to be stable if the initial order of equal elements is preserved in the final sorted permutation. In-place sorting algorithms are generally unstable because there's no way to recover the original order of equal values when solely inspecting the value of an element. Assigning a unique value to each item makes an unstable sort stable, since it effectively eliminates all equal items, but require an additional n space.