# 9: Sorting Algorithms

- Page ID
- 47930

\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

\( \newcommand{\Span}{\mathrm{span}}\)

\( \newcommand{\id}{\mathrm{id}}\)

\( \newcommand{\Span}{\mathrm{span}}\)

\( \newcommand{\kernel}{\mathrm{null}\,}\)

\( \newcommand{\range}{\mathrm{range}\,}\)

\( \newcommand{\RealPart}{\mathrm{Re}}\)

\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

\( \newcommand{\Argument}{\mathrm{Arg}}\)

\( \newcommand{\norm}[1]{\| #1 \|}\)

\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)

\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)

\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

\( \newcommand{\vectorC}[1]{\textbf{#1}} \)

\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

## Unit Introduction

A sorting algorithm is an algorithm that puts elements of a collection such as array or list in a certain order. The most used orders are numerical order and lexicographical order. Efficient sorting is important to optimizing the use of other algorithms, such as in search and merge algorithms that require sorted collections to work correctly. It is also often useful for normalization of data and for producing human-readable output. More precisely, the output must satisfy two conditions (i) the output is in non-decreasing order (each element is no smaller than the previous element according to the desired total order) and (ii) the output is a permutation, or reordering, of the input.

The Sorting algorithms used in computer science are often classified by:

- Computational complexity (worst, average and best case behavior) in terms of the size of the list (n) - For typical sorting algorithms good behavior is O(n log n) and bad behavior is O(n2). Ideal behavior for a sort is O(n). Sort algorithms which only use an abstract key comparison operation often require at least O(n log n) comparisons on average.

- Memory usage (and use of other computer resources) - In particular, some sorting algorithms are “in place”, such that little memory is needed beyond the items being sorted, while others need to create auxiliary locations for data to be temporarily stored.

- Stability - Stable sorting algorithms maintain the relative order of records with equal keys (i.e. values). That is, a sorting algorithm is stable if whenever there are two records R and S with the same key and with R appearing before S in the original list, R will appear before S in the sorted list.

- Whether or not is a comparison sort - A comparison sort examines the data only by comparing two elements with a comparison operator.

- General sort method – whether uses insertion, exchange, selection, merging, etc.

Table \(\PageIndex{1}\) provides a summary of some sorting algorithms and their classification. When equal elements are indistinguishable, such as with integers, stability is not an issue. However, assume that the following pairs of numbers are to be sorted by their first coordinate: (4, 1) (3, 1) (3, 7) (5, 6). In this case, two different results are possible, that is, one which maintains the relative order of records with equal keys, and one which does not as shown below:

**(3, 1) (3, 7) (4, 1) (5, 6) (order maintained)**

**(3, 7) (3, 1) (4, 1) (5, 6) (order changed)**

Unstable sorting algorithms may change the relative order of records with equal keys, but stable sorting algorithms never do so. Unstable sorting algorithms can be specially implemented to be stable. One way of doing this is to artificially extend the key comparison, so that comparisons between two objects with otherwise equal keys are decided using the order of the entries in the original data order as a tie-breaker. Remembering this order, however, often involves an additional space cost.

Table \(\PageIndex{1}\), presents a classification summary of the sorting algorithms considered in this unit. In the table, n is the number of records to be sorted. The columns “Best”, “Average”, and “Worst” give the time complexity in each case. “Memory” denotes the amount of auxiliary storage needed beyond that used by the collection list itself. “Cmp” indicates whether the sort is a comparison sort.

**Table \(\PageIndex{1}\):** A Sorting Algorithms Classification

Name |
Best |
Average |
Worst |
Memory |
Stable |
Cmp |
Method |
---|---|---|---|---|---|---|---|

Selection sort |
O(n2) |
O(n2) |
O(n2) |
O(1) |
No |
Yes |
Selection |

Insertion sort |
O(n) |
— |
O(n2) |
O(1) |
Yes |
Yes |
Insertion |

Binary tree sort |
O(nlog(n)) |
— |
O(nlog(n)) |
O(1) |
Yes |
Yes |
Insertion |

Merge sort |
O(nlog(n)) |
— |
O(nlog(n)) |
O(n) |
Yes |
Yes |
Merging |

Heap sort |
O(nlog(n)) |
— |
O(nlog(n)) |
O(1) |
No |
Yes |
Selection |

Quick sort |
O(nlog(n)) |
O(nlog(n)) |
O(n2) |
O(log n) |
No |
Yes |
Partitioning |

Unit Objectives

Upon completion of this unit you should be able to:

- Demonstrate understanding of various sorting algorithm
- Select best sorting method based on performance requirement
- Apply sorting algorithms in problem solving

## Key Terms

**Sorting**: A process that puts elements of a collection such as array or list in a certain order