Module: src/algorithms
This doc covers each sorting algorithm in src/algorithms/sorting-algorithms.ts: what it does, time/space complexity, when to use it, and minimal code examples.
Method: SortingAlgorithms.bubbleSort(arr, compare?)
Repeatedly steps through the array, compares adjacent elements, and swaps them if they are in the wrong order. Passes continue until no swaps are needed.
- Time: O(n²) average and worst. O(n) when already sorted (with early exit).
- Space: O(1) on the copied array.
- Learning and teaching only. Do not use in production for non-tiny inputs.
- Any real data: use merge sort, quick sort, or heap sort (or the engine’s built-in sort).
import { SortingAlgorithms } from "@/algorithms/sorting-algorithms";
const arr = [64, 34, 25, 12, 22, 11, 90];
const sorted = SortingAlgorithms.bubbleSort(arr);
// sorted: [11, 12, 22, 25, 34, 64, 90]; arr unchangedMethod: SortingAlgorithms.insertionSort(arr, compare?)
Builds the sorted result one element at a time: for each element, inserts it into the correct position in the already-sorted prefix.
- Time: O(n²) worst. O(n) when input is nearly sorted.
- Space: O(1) on the copy.
- Very small arrays (e.g. n < 50).
- Input is almost sorted (e.g. re-sorting after a few insertions).
- Large or random data: use O(n log n) sorts.
const sorted = SortingAlgorithms.insertionSort([3, 1, 4, 1, 5]);
// [1, 1, 3, 4, 5]Method: SortingAlgorithms.selectionSort(arr, compare?)
Repeatedly finds the minimum of the unsorted region and swaps it to the front. Simple but always Θ(n²) comparisons.
- Time: O(n²).
- Space: O(1) on the copy.
- Learning, or when writes/swaps are expensive and you want to minimize them (at most n−1 swaps).
- General-purpose sorting: use merge/quick/heap.
const sorted = SortingAlgorithms.selectionSort([29, 10, 14, 37, 13]);
// [10, 13, 14, 29, 37]Method: SortingAlgorithms.mergeSort(arr, compare?)
Divide and conquer: split in half, sort each half (recursively), then merge the two sorted halves. Stable (equal elements keep relative order).
- Time: O(n log n) always.
- Space: O(n) for temporary arrays during merge.
- When you need stable sort or guaranteed O(n log n) (e.g. avoid quicksort’s worst case).
- Linked lists (merge sort is natural and O(1) extra space with pointer rewiring).
- When in-place and non-stable is fine: quick sort or heap sort may be faster in practice.
const sorted = SortingAlgorithms.mergeSort([38, 27, 43, 3, 9, 82, 10]);
// [3, 9, 10, 27, 38, 43, 82]Method: SortingAlgorithms.quickSort(arr, compare?)
Chooses a pivot (this implementation uses the last element), partitions the array so smaller elements are left and larger are right of the pivot, then recurses on both sides.
- Time: O(n log n) average. O(n²) worst case (e.g. sorted or reverse-sorted input with last-element pivot).
- Space: O(log n) stack for recursion.
- General-purpose in-memory sort when you don’t need stability; often fastest in practice.
- Random or shuffled data to reduce worst-case risk.
- When you need stability: use merge sort.
- When you must guarantee O(n log n): use merge or heap sort.
const sorted = SortingAlgorithms.quickSort([5, 2, 9, 1, 7, 6]);
// [1, 2, 5, 6, 7, 9]Method: SortingAlgorithms.heapSort(arr, compare?)
Builds a max-heap from the array, then repeatedly extracts the maximum (swap with end, reduce heap size, heapify down). Sorts in place on the copy.
- Time: O(n log n) always.
- Space: O(1) on the copy.
- When you need guaranteed O(n log n) and in-place (e.g. limited memory).
- When you want to avoid quicksort’s worst case and don’t need stability.
- When you need stable sort: use merge sort.
- When average-case speed matters more than worst case: quick sort is often faster.
const sorted = SortingAlgorithms.heapSort([12, 11, 13, 5, 6, 7]);
// [5, 6, 7, 11, 12, 13]| Algorithm | Time (avg) | Time (worst) | Space | Stable |
|---|---|---|---|---|
| Bubble | O(n²) | O(n²) | O(1) | Yes |
| Insertion | O(n²) | O(n²) | O(1) | Yes |
| Selection | O(n²) | O(n²) | O(1) | No |
| Merge | O(n log n) | O(n log n) | O(n) | Yes |
| Quick | O(n log n) | O(n²) | O(log n) | No |
| Heap | O(n log n) | O(n log n) | O(1) | No |
- Main README — documentation index and links to all algorithm docs
- Module README: src/algorithms/README.md
- docs/sorting-data-generator.md — generate
sorting-data.jsonfor correctness and optional performance tests.