A Systematic Approach to Algorithms

Vijay K. Garg · The University of Texas at Austin

Chapter 5. Sorting Algorithms

Comparison-based sorting algorithms in classical imperative form — the schedules underlying the LLP forms.

This page: Classical forms. View LLP forms »

This page collects the classical / sequential implementations of the algorithms developed in this chapter. The lattice-linear (LLP) reformulations and chapter setup live on the LLP companion page.

BubbleSort

Repeated passes of adjacent compare-and-swap until a complete pass makes no swaps.

Time complexity: $O(n^2)$, where $n$ is the size of the array.

InsertionSort

Builds a sorted prefix by walking each new element down to its correct slot via adjacent swaps.

Time complexity: $O(n^2)$, where $n$ is the size of the array.

Merge

Out-of-place merge of two sorted arrays into a third. The basic building block of MergeSort.

Time complexity: $O(n_1 + n_2)$, where $n_1$ and $n_2$ are the sizes of the two input arrays.

MergeSort

Sequential merge sort. The two recursive calls on the halves are composed with ; instead of [ ... [] ... ]; the parallel form lives in Chapter 10. The demo runs the bottom-up schedule of merges — sizes $1, 2, 4, \ldots$ — one merge per Step.

Time complexity: $O(n \log n)$, where $n$ is the size of the array.

QuickSort

Sequential QuickSort with Lomuto partition (pivot = rightmost element). The two recursive calls on the partition halves are composed with ;; the parallel form lives in Chapter 10. The demo maintains a stack of pending intervals — each Step pops the top and partitions it.

Time complexity: $O(n^2)$ worst case, $O(n \log n)$ on average, where $n$ is the size of the array.

ThreeWayPartition

Dutch-flag partition: splits $A[\ell..h)$ into less-than-pivot, equal-to-pivot, and greater-than-pivot regions. Used by Quicksort variants.

Time complexity: $O(n)$, where $n$ is the size of the input range.