Algorithms are step-by-step computational procedures for solving problems β ranging from simple tasks like searching and sorting to complex optimization and graph traversal challenges. They form the foundation of computer science, enabling efficient data processing across fields like artificial intelligence, databases, networking, and systems design. Understanding algorithmic paradigms β such as divide-and-conquer, dynamic programming, greedy methods, and backtracking β equips you to select the right strategy for a problem's constraints. A key insight: the best algorithm isn't always the fastest in theory; cache locality, constant factors, and input characteristics often matter more in practice than asymptotic complexity alone.
What This Cheat Sheet Covers
This topic spans 21 focused tables and 166 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Sorting Algorithms
| Algorithm | Example | Description |
|---|---|---|
pivot = arr[high]partition(arr, low, high)quicksort(arr, low, pi-1) | β’ Divide-and-conquer sort that picks a pivot, partitions around it, and recursively sorts β’ average O(n \log n), worst O(n^2) but cache-friendly and widely used in practice. | |
mid = (left + right) // 2mergesort(arr, left, mid)merge(arr, left, mid, right) | β’ Stable divide-and-conquer sort that always runs in O(n \log n) timeβ’ requires O(n) extra space but guarantees consistent performance. | |
minrun = compute_minrun(n)insertionSort for runs < minrunmerge adjacent runs | β’ Hybrid of merge sort and insertion sort β’ O(n \log n) worst case, optimized for real-world data, used in Python and Java standard libraries. | |
buildMaxHeap(arr)swap(arr[0], arr[i])heapify(arr, 0, i) | β’ In-place sort using a binary heap β’ O(n \log n) time, O(1) space, but not stable and slower in practice than Quick Sort due to poor cache behavior. | |
count = [0] * (max_val + 1)for x in arr: count[x] += 1for i, c in enumerate(count): output.extend([i] * c) | β’ Non-comparison integer sort that counts occurrences β’ O(n + k) time where k is range, efficient when k = O(n) but requires extra space. | |
for digit in range(num_digits): countingSortByDigit(arr, digit) | β’ Non-comparison sort that processes digits from least to most significant β’ O(d(n + k)) where d is digits, works well for integers with bounded digit count. |