Merge Sort visualization
Divide in half, sort each half recursively, then merge the two sorted halves.
Divide and conquer: split until pieces have one element, then merge sorted pieces back together by repeatedly taking the smaller front element.
Always O(n log n) and stable, at the cost of O(n) extra memory for the merge buffer.
Complexity
Time: best O(n log n), average O(n log n), worst O(n log n). Space: O(n). Difficulty: Intermediate.
Pseudocode
procedure mergeSort(A, lo, hi):
if lo ≥ hi: return
mid ← (lo + hi) / 2
mergeSort(A, lo, mid); mergeSort(A, mid+1, hi)
L ← A[lo..mid]; R ← A[mid+1..hi]
while L and R not empty:
take smaller of L[i], R[j] into A[k]
copy the leftovers into AMore sorting algorithms
- Bubble Sort — Repeatedly swap adjacent out-of-order pairs; the largest value bubbles to the end each pass.
- Selection Sort — Find the minimum of the unsorted part and swap it to the front.
- Insertion Sort — Grow a sorted prefix by inserting each new value into its correct slot.
- Cocktail Shaker Sort — Bubble sort that alternates direction, moving large values right and small values left.
- Shell Sort — Insertion sort over shrinking gaps, so values travel long distances early.
- Quick Sort — Partition around a pivot so smaller values go left and larger go right, then recurse.
- Heap Sort — Build a max-heap, then repeatedly move the root (maximum) to the end and re-heapify.
- Counting Sort — Count occurrences of each value, then rebuild the array from the counts.
- Radix Sort (LSD) — Sort by the ones digit, then tens, then hundreds — each pass is a stable bucket sort.