Sorting algorithms & applications

Applications

Sorting is also one of the most frequently performed computing tasks, e.g.,

Sorting algorithms

Most sorting algorithms rely on comparisons of key values, except for some special algorithms such as binsort, bucket sort & radix sort, which we'll see below.

Mergesort

A divide and conquer algorithm. Mergesort splits the list in half, sort the halves, and then merge the sorted halves together. How merge works? The merge step compares the front elements of the two sublists and continually appending the smaller to the output list until no more input elements remain.
Mergesort lends itself well to sorting a singly linked list because merging does not require random access to the list elements. Thus, Mergesort is the method of choice when the input is in the form of a linked list.

Cost of mergesort? At each of the log n levels of recursion, $\Theta(n)$ work is done, for a total cost of $\Theta(n log n)$. This cost is unaffected by the relative order of the values being sorted, thus this analysis holds for the best, average, and worst cases.

Quicksort

Heapsort

$\Theta(n^2)$ sorting algorithms

"Linear" time sorting algorithms

Comparison of sorting algorithms

Asymptotic time complexity

Selection/insertion/bubbleQuicksortMergesortHeapsortRadix sort
Best case$\Theta(n^2)$$\Theta(nlogn)$$\Theta(nlogn)$$\Theta(nlogn)$
Average case$\Theta(n^2)$$\Theta(nlogn)$$\Theta(nlogn)$$\Theta(nlogn)$
Worst case$\Theta(n^2)$$\Theta(n^2)$ (unlikely)$\Theta(nlogn)$$\Theta(nlogn)$

Lower bounds for comparison algorithms


Side note: why do we care about comparison operations in sorting algorithms?

We would like to evaluate the time complexity of sorting algorithms even if we consider only comparison operations as operation(s) in our sorting:
<, <=, >, >=, ==
If we consider only comparison operations, we can visualize such an algorithm as a "tree of all possible comparison outcomes".

• For example, for a binary search of a value x within a sorted array A containing n elements, we could visualize all the possible choices as a "decision tree" representing the algorithm, thus (here for n=3) :

In this case, when we consider only comparisons, one run of the algorithm is equivalent to traversing the tree from the root to a leaf.
In general, the number of possible answers is >= n , so we have at least n leaves in the tree.
In general, the height of the path in the binary search tree is >= (log n), so we can consider (log n) as a lower bound (can't be less than).
The worst-case running time of the binary search algorithm is $\Omega(log n)$.

• What about the worst-case running time of the sorting algorithm?
(we could also visualize the sorting algorithm in the same way, but it would be a much bigger tree!)
In general, the number of possible answers in the sorting algorithm is >= n! i.e. the number of possible permutations of n elements, which is the number of possible leaves in the decision tree for a sorting algorithm that uses comparisons.
The height of the sorting algorithm search tree is >= (log n!), so we can consider (log n!) as a lower bound (can't be less than) of necessary operations for sorting n elements, i.e. the lower bound is $\Omega(n log n)$.