LESSON 04 · UNIT 4 AOS 1 ANALYSIS, RECURRENCES & PARADIGMS

Asymptotic Analysis, Recurrence Relations, Master Theorem & Sorting

Unit 4 formalises algorithm efficiency independent of physical hardware using the Word RAM model, asymptotic growth classes (O,Ω,Θ\mathcal{O}, \Omega, \Theta), closed-form recurrence derivations, the Master Theorem, and structural design paradigms.

01 · MACHINE-INDEPENDENT ANALYSIS

word ram model, basic operations & big-o bounds.

In theoretical computer science, algorithms are analysed on the Word RAM (Random Access Machine) model: a hypothetical computer where memory consists of an unbounded array of words, and primitive operations (arithmetic +,−,×,÷+, -, \times, \div, logical comparisons, array indexing, and variable assignment) on word-sized operands execute in Θ(1)\Theta(1) constant time.

Rather than counting every machine instruction, we identify the Basic Operation—the most significant operation inside the deepest loop (such as key comparison in sorting/searching, or arithmetic multiplication in matrix algorithms)—and express its count f(n)f(n) as a growth function of the input size nn across Best-Case, Worst-Case, and Average-Case inputs. Note that in the HESS notes:

  • Growth Function Restrictions: Any valid complexity growth function f(n)f(n) must satisfy two mathematical domain/range restrictions: n∈N={1,2,3,… }n \in \mathbb{N} = \{1, 2, 3, \dots\} (input sizes are positive integers) and ∀n∈N,  f(n)≥0\forall n \in \mathbb{N},\; f(n) \ge 0 (step counts are non-negative).
  • Average-Case Complexity Warning: The Average-Case complexity is determined using probability and statistical expectation over the distribution of all possible inputs of size nn—it is not simply the arithmetic mean of the Best Case and Worst Case!
HESS CANONICAL BASIC OPERATIONS BY DOMAIN
ALGORITHM CLASS BASIC OPERATION
Sorting / Searching Key Comparison (if A[i] == A[j])
Matrix Multiplication Float / Int Multiplication (M[i][j] = a * b)
Graph Traversal Visiting / Expanding a Node
HESS SPACE COMPLEXITY REFERENCE RULES
PROGRAM CONSTRUCT SPACE COMPLEXITY
Initialising / Assigning Variable O(1)\mathcal{O}(1)
Storing Array / List / Stack / Queue of size nn O(n)\mathcal{O}(n)
Iterative Function Calls O(1)\mathcal{O}(1)
nn Nested Recursive Function Calls O(n)\mathcal{O}(n) (Call Stack Frames)
FORMAL ASYMPTOTIC DEFINITIONS UPPER · LOWER · TIGHT
  • Big-O (f(n)∈O(g(n))f(n) \in \mathcal{O}(g(n))): Formal upper bound. There exist positive constants c>0c > 0 and n0≥1n_0 \ge 1 such that 0≤f(n)≤c⋅g(n)0 \le f(n) \le c \cdot g(n) for all n≥n0n \ge n_0.
  • Big-Omega (f(n)∈Ω(g(n))f(n) \in \Omega(g(n))): Formal lower bound. There exist positive constants c>0c > 0 and n0≥1n_0 \ge 1 such that f(n)≥c⋅g(n)≥0f(n) \ge c \cdot g(n) \ge 0 for all n≥n0n \ge n_0.
  • Big-Theta (f(n)∈Θ(g(n))f(n) \in \Theta(g(n))): Formal tight bound. f(n)∈Θ(g(n))  ⟺  f(n)∈O(g(n))f(n) \in \Theta(g(n)) \iff f(n) \in \mathcal{O}(g(n)) and f(n)∈Ω(g(n))f(n) \in \Omega(g(n)) (bounded both above and below by g(n)g(n) for all n≥n0n \ge n_0).
SPACE & AUXILIARY COMPLEXITY MEMORY FOOTPRINT
  • Total Space Complexity: Total memory cells consumed by the algorithm, including the input data itself.
  • Auxiliary Space Complexity: Extra temporary working memory allocated beyond the input data (e.g., call-stack frames in recursion, or temporary subarrays in Merge Sort).
  • In-Place Algorithm: An algorithm that requires only O(1)\mathcal{O}(1) (or O(log⁡n)\mathcal{O}(\log n) stack) auxiliary space, modifying the input container directly.
02 · CLOSED-FORM DERIVATIONS

solving recurrence relations: iteration & telescoping.

The running time of a recursive algorithm is naturally expressed as a recurrence relation consisting of a recursive step (for n>n0n > n_0) and a base case (e.g., T(1)=cT(1) = c). Note that by Church-Turing equivalence, every recursive function can be converted into an iterative function (using an explicit loop/stack) and vice versa. In VCE Algorithmics, recurrences that are not in Master Theorem form (such as decrease-by-one T(n−1)T(n - 1) recurrences) are solved into closed form via Iteration (Backward/Forward Substitution) or Telescoping Sums.

Essential Closed-Form Summation Identities

Triangular Numbers (Arithmetic Series): ∑i=1ni=1+2+⋯+n=n(n+1)2∈Θ(n2)\text{Triangular Numbers (Arithmetic Series): } \sum_{i=1}^{n} i = 1 + 2 + \dots + n = \frac{n(n+1)}{2} \in \Theta(n^2)
Geometric Series (Powers of 2): ∑i=0n−12i=1+2+4+⋯+2n−1=2n−1∈Θ(2n)\text{Geometric Series (Powers of 2): } \sum_{i=0}^{n-1} 2^i = 1 + 2 + 4 + \dots + 2^{n-1} = 2^n - 1 \in \Theta(2^n)
EXAMPLE 1 · ARITHMETIC ITERATION T(n) = T(n-1) + 4, T(1) = 1

Substitute backward for kk steps:

T(n)=T(n−1)+4=(T(n−2)+4)+4=T(n−2)+2(4)=T(n−k)+4k\begin{aligned} T(n) &= T(n-1) + 4 \\ &= (T(n-2) + 4) + 4 = T(n-2) + 2(4) \\ &= T(n-k) + 4k \end{aligned}

Set n−k=1  ⟹  k=n−1n - k = 1 \implies k = n - 1:

T(n)=T(1)+4(n−1)=1+4n−4=4n−3∈Θ(n)T(n) = T(1) + 4(n - 1) = 1 + 4n - 4 = 4n - 3 \in \Theta(n)
EXAMPLE 2 · GEOMETRIC ITERATION T(n) = 2T(n-1) + 1, T(1) = 1

Expand the recurrence step-by-step for kk steps until reaching T(1)T(1):

T(n)=2T(n−1)+1=22T(n−2)+2+1=2kT(n−k)+∑i=0k−12i\begin{aligned} T(n) &= 2T(n-1) + 1 = 2^2 T(n-2) + 2 + 1 \\ &= 2^k T(n-k) + \sum_{i=0}^{k-1} 2^i \end{aligned}

Set n−k=1  ⟹  k=n−1n - k = 1 \implies k = n - 1:

T(n)=2n−1T(1)+(2n−1−1)=2n−1∈Θ(2n)T(n) = 2^{n-1} T(1) + (2^{n-1} - 1) = 2^n - 1 \in \Theta(2^n)
EXAMPLE 3 · TRIANGULAR ITERATION / TELESCOPING T(n) = T(n-1) + n, T(1) = 1

Rearrange as T(i)−T(i−1)=iT(i) - T(i-1) = i and sum from i=2i = 2 to nn:

T(n)−T(n−1)=nT(n−1)−T(n−2)=n−1    ⋮T(2)−T(1)=2\begin{aligned} T(n) - T(n-1) &= n \\ T(n-1) - T(n-2) &= n - 1 \\ &\;\;\vdots \\ T(2) - T(1) &= 2 \end{aligned}

Summing the left and right sides telescopes to:

T(n)−T(1)=∑i=2ni  ⟹  T(n)=∑i=1ni=n(n+1)2∈Θ(n2)T(n) - T(1) = \sum_{i=2}^{n} i \implies T(n) = \sum_{i=1}^{n} i = \frac{n(n+1)}{2} \in \Theta(n^2)
EXAMPLE 4 · CONSTANT TELESCOPING & LOGARITHMIC HALVING Tn = Tn-1 + 2 (T0=1) & BINARY SEARCH

Constant Telescoping (Tn=Tn−1+2,  T0=1T_n = T_{n-1} + 2,\; T_0 = 1): Writing Ti−Ti−1=2T_i - T_{i-1} = 2 for i=1…ni = 1 \dots n and summing cancels all interior terms:

Tn−T0=∑i=1n2=2n  ⟹  Tn=1+2n∈Θ(n)T_n - T_0 = \sum_{i=1}^{n} 2 = 2n \implies T_n = 1 + 2n \in \Theta(n)

Binary Search by Iteration (T(n)=T(n/2)+O(1)T(n) = T(n/2) + \mathcal{O}(1)): After k=log⁡2nk = \log_2 n halvings (n/2k=1n / 2^k = 1):

T(n)=T(1)+log⁡2(n)⋅O(1)=O(log⁡n)T(n) = T(1) + \log_2(n) \cdot \mathcal{O}(1) = \mathcal{O}(\log n)
03 · DIVIDE-AND-CONQUER ASYMPTOTICS

the master theorem & strassen's multiplication.

For divide-and-conquer algorithms that divide a problem of size nn into a≥1a \ge 1 subproblems, each of size n/bn / b (where b>1b > 1), and spend f(n)∈Θ(nk)f(n) \in \Theta(n^k) work splitting and merging (where k≥0k \ge 0):

T(n)=a T ⁣(nb)+f(n)where f(n)∈Θ(nk)T(n) = a\,T\!\left(\frac{n}{b}\right) + f(n) \qquad \text{where } f(n) \in \Theta(n^k)
T(n)∈{Θ(nk)if a<bk(Case 1: Root split/merge work dominates)Θ(nklog⁡n)if a=bk(Case 2: Equal work across all log⁡bn tree levels)Θ ⁣(nlog⁡ba)if a>bk(Case 3: Leaf subproblem count dominates)T(n) \in \begin{cases} \Theta(n^k) & \text{if } a < b^k \quad \text{(Case 1: Root split/merge work dominates)} \\[6pt] \Theta(n^k \log n) & \text{if } a = b^k \quad \text{(Case 2: Equal work across all } \log_b n \text{ tree levels)} \\[6pt] \Theta\!\left(n^{\log_b a}\right) & \text{if } a > b^k \quad \text{(Case 3: Leaf subproblem count dominates)} \end{cases}
CANONICAL MASTER THEOREM APPLICATIONS IN VCE ALGORITHMICS
ALGORITHM RECURRENCE T(n)T(n) PARAMETERS (a,b,ka, b, k) COMPARISON CLOSED-FORM BOUND
Binary Search T(n)=T(n/2)+1T(n) = T(n/2) + 1 a=1,b=2,k=0a=1, b=2, k=0 1=201 = 2^0 (Case 2) Θ(n0log⁡n)=Θ(log⁡n)\Theta(n^0 \log n) = \Theta(\log n)
Merge Sort / Quick Sort (Best) T(n)=2T(n/2)+nT(n) = 2T(n/2) + n a=2,b=2,k=1a=2, b=2, k=1 2=212 = 2^1 (Case 2) Θ(nlog⁡n)\Theta(n \log n)
Binary Tree Traversal T(n)=2T(n/2)+1T(n) = 2T(n/2) + 1 a=2,b=2,k=0a=2, b=2, k=0 2>202 > 2^0 (Case 3) Θ(nlog⁡22)=Θ(n)\Theta(n^{\log_2 2}) = \Theta(n)
Naive D&C Matrix Multiply T(n)=8T(n/2)+n2T(n) = 8T(n/2) + n^2 a=8,b=2,k=2a=8, b=2, k=2 8>228 > 2^2 (Case 3) Θ(nlog⁡28)=Θ(n3)\Theta(n^{\log_2 8}) = \Theta(n^3)
Strassen's Matrix Multiply T(n)=7T(n/2)+n2T(n) = 7T(n/2) + n^2 a=7,b=2,k=2a=7, b=2, k=2 7>227 > 2^2 (Case 3) Θ(nlog⁡27)≈Θ(n2.81)\Theta(n^{\log_2 7}) \approx \Theta(n^{2.81})
Master Theorem Divide-and-Conquer Recursion Tree Recursion tree for T(n) = aT(n/b) + f(n) showing depth log_b(n), root work f(n), level-1 work a*f(n/b), and n^(log_b a) leaves. Size n : f(n) T(n/b) T(n/b) ••• (a×) T(1) T(1) ••• T(1) T(1) L0: c · n^k (Case 1) L1: a · c(n/b)^k Height = log_b(n) Leaves: n^(log_b a)
FIGURE 04 · MASTER THEOREM RECURSION TREE: ROOT WORK (CASE 1) VS EQUAL LEVELS (CASE 2) VS LEAVES (CASE 3)
WHEN THE MASTER THEOREM CANNOT BE APPLIED & FALLBACK METHODS EXAM TRAP & EXTENSIONS

The Master Theorem does not apply when: (1) the problem size decreases by subtraction rather than division (e.g., T(n)=T(n−1)+nT(n) = T(n-1) + n), (2) subproblems have uneven split sizes (e.g., T(n)=T(n/2)+T(n/4)+2nT(n) = T(n/2) + T(n/4) + 2n), (3) aa is not a constant, or (4) the non-recursive work f(n)f(n) is non-polynomial (e.g., f(n)=2nf(n) = 2^n). When the Master Theorem fails, the HESS notes specify three fallback methods: the Recursion Tree Method (summing work level-by-level), the Substitution Method (guessing the bound and proving by mathematical induction), or the general Akra–Bazzi Theorem (for uneven divide-and-conquer splits).

04 · DECREASE-BY-A-CONSTANT-FACTOR

binary search & loop invariant induction proof.

Binary Search locates a target value xx in a sorted array A[0…n−1]A[0 \dots n-1] by comparing xx against the midpoint m=⌊(min+max)/2⌋m = \lfloor (\text{min} + \text{max}) / 2 \rfloor and discarding half of the remaining array each step.

In the HESS notes, Binary Search is rigorously classified as a Decrease-and-Conquer algorithm (specifically decrease-by-a-constant-factor) rather than Divide-and-Conquer, because it only recurses on one half (a=1a = 1) and discards the other without any merge step.

BINARY SEARCH · ITERATIVE PSEUDOCODE Θ(log n) WORST CASE
Algorithm BinarySearch(A[0..n-1], target):
    min <- 0
    max <- n - 1
    While min <= max do:
        mid <- floor((min + max) / 2)
        If A[mid] == target then:
            Return mid
        Else If A[mid] < target then:
            min <- mid + 1
        Else:
            max <- mid - 1
        End If
    End While
    Return -1 // Not found
End Algorithm
LOOP INVARIANT PROOF BY INDUCTION HESS CORRECTNESS PROOF

Invariant P(k)P(k): If target is present in the sorted array AA, then at step kk, A[min]≤target≤A[max]A[\text{min}] \le \text{target} \le A[\text{max}].

  • Base Case (k=0k = 0): Before the first iteration, min=0\text{min} = 0 and max=n−1\text{max} = n - 1. Since AA is sorted across 0…n−10 \dots n-1, if target is in AA, then A[0]≤target≤A[n−1]A[0] \le \text{target} \le A[n-1] holds.
  • Inductive Step (P(k)  ⟹  P(k+1)P(k) \implies P(k+1)): Assume A[min]≤target≤A[max]A[\text{min}] \le \text{target} \le A[\text{max}]. Let mid=⌊(min+max)/2⌋\text{mid} = \lfloor(\text{min}+\text{max})/2\rfloor:
    • If A[mid]<targetA[\text{mid}] < \text{target}, because AA is sorted, A[mid+1]≤targetA[\text{mid}+1] \le \text{target}. Setting min′=mid+1\text{min}' = \text{mid}+1 preserves A[min′]≤target≤A[max]A[\text{min}'] \le \text{target} \le A[\text{max}].
    • If A[mid]>targetA[\text{mid}] > \text{target}, because AA is sorted, target≤A[mid−1]\text{target} \le A[\text{mid}-1]. Setting max′=mid−1\text{max}' = \text{mid}-1 preserves A[min]≤target≤A[max′]A[\text{min}] \le \text{target} \le A[\text{max}'].
  • Termination: Since the interval size max−min+1\text{max} - \text{min} + 1 strictly decreases each step, the loop must terminate either by finding A[mid]==targetA[\text{mid}] == \text{target} or exhausting min>max\text{min} > \text{max}.
05 · STRUCTURAL PROBLEM SOLVING

algorithm design paradigms & greedy proof methods.

THE SIX CORE ALGORITHM DESIGN PARADIGMS (HESS NOTES)
PARADIGM CORE MECHANISM CANONICAL EXAMPLES
Brute-Force (Exhaustive Search) Generates and evaluates every candidate solution in the state space directly from the problem statement. Linear Search, Selection Sort, Exhaustive TSP (O(n!)\mathcal{O}(n!)), 0-1 Knapsack Power Set (O(2n)\mathcal{O}(2^n)).
Greedy Paradigm Builds a solution step-by-step by making the locally optimal choice at each stage, never reconsidering past choices. Prim's MST, Kruskal's MST, Dijkstra's SSSP, Greedy Graph Colouring, Coin Change (canonical denominations).
Decrease-and-Conquer Reduces a problem instance to a single smaller instance (by a constant n−1n-1, constant factor n/2n/2, or variable size) and extends the solution. Binary Search (n/2n/2), Insertion Sort (n−1n-1), DFS/BFS, Euclidean GCD (variable decrease).
Divide-and-Conquer Splits a problem into multiple independent subproblems of roughly equal size, solves them recursively, and merges the results. Merge Sort, Quick Sort, Binary Tree Traversal, Strassen's Matrix Multiplication, Closest Pair of Points.
Dynamic Programming (DP) Optimises problems with overlapping subproblems and optimal substructure by caching subproblem solutions (bottom-up tabulation or top-down memoization). Fibonacci (tabulated), Bellman-Ford, Warshall's, Floyd-Warshall's, 0-1 Knapsack DP.
Backtracking Optimised brute-force search over a state-space tree that prunes (abandons) partial solutions as soon as they violate constraints. NN-Queens Problem, Exact Graph Colouring, Hamiltonian Circuit search, Sudoku solver.

Proving Greedy Algorithms Correct (The Two 4-Step HESS Proof Frameworks)

Because greedy algorithms do not always yield globally optimal solutions (for example, greedy coin-changing fails for coins {1,3,4}\{1, 3, 4\} with target 66, picking 4+1+14+1+1 instead of 3+33+3), proving a greedy algorithm optimal requires one of two formal 4-step proof frameworks from the HESS notes:

METHOD 1 · GREEDY STAYS AHEAD (4 STEPS) INDUCTIVE DOMINANCE
  1. Define Solutions: Let X=(x1,x2,…,xk)X = (x_1, x_2, \dots, x_k) be the greedy solution and X∗=(x1∗,x2∗,…,xm∗)X^* = (x_1^*, x_2^*, \dots, x_m^*) be an arbitrary optimal solution.
  2. Define Measure: Define a quantitative progress measure mi(X)m_i(X) and mi(X∗)m_i(X^*) after step ii.
  3. Prove Greedy Stays Ahead: Prove by mathematical induction on ii that mi(X)≥mi(X∗)m_i(X) \ge m_i(X^*) (or ≤\le for minimisation) for all ii.
  4. Prove Optimality: Deduce (often by contradiction) that because greedy stays ahead at termination, XX must be globally optimal.
METHOD 2 · EXCHANGE ARGUMENT (4 STEPS) ITERATIVE TRANSFORMATION
  1. Define Solutions: Let XX be the greedy solution and X∗X^* be a hypothetical optimal solution.
  2. Compare Solutions: Assume X≠X∗X \neq X^* and identify the first component where X∗X^* differs from XX.
  3. Exchange Pieces: Swap the differing element in X∗X^* with the greedy choice from XX to form X′X', proving X′X' remains valid and no worse in cost than X∗X^*.
  4. Iterate: Repeat the exchange step until X∗X^* is transformed into XX without degrading optimality, proving XX is optimal.
ALGORITHM · RECURSIVE BACKTRACKING TEMPLATE HESS STATE-SPACE PRUNING PSEUDOCODE
Algorithm BackTrack(attempt, possible_options):
    If attempt is a complete solution then:
        Return attempt
    End if
    For each option in possible_options do:
        If option is valid for attempt then:
            add option to attempt
            result <- BackTrack(attempt, updated_options)
            If result != failure then:
                Return result
            End if
            remove option from attempt // Backtrack!
        End if
    End do
    Return failure
End Algorithm
06 · DIVIDE-AND-CONQUER SORTING

merge sort & quick sort analysis.

MERGE SORT · TOP-DOWN PSEUDOCODE STABLE · NOT IN-PLACE · Θ(n log n)
Algorithm MergeSort(A[0..n-1]):
    Function Merge(Al[0..p-1], Ar[0..q-1], A[0..p+q-1]):
        i <- 0; j <- 0; k <- 0
        While i < p and j < q do:
            If Al[i] <= Ar[j] then:
                A[k] <- Al[i]; i <- i + 1
            Else:
                A[k] <- Ar[j]; j <- j + 1
            End If
            k <- k + 1
        End While
        If i == p then:
            Copy Ar[j..q-1] to A[k..p+q-1]
        Else:
            Copy Al[i..p-1] to A[k..p+q-1]
        End If
    End Function

    If n > 1 then:
        mid <- floor(n / 2)
        Copy A[0..mid-1] to Al
        Copy A[mid..n-1] to Ar
        MergeSort(Al)
        MergeSort(Ar)
        Merge(Al, Ar, A)
    End If
End Algorithm
QUICK SORT · HOARE PARTITIONING IN-PLACE · UNSTABLE · O(n²) WORST / Θ(n log n) AVG
Algorithm Quicksort(A, min, max):
    Function Partition(A, min, max):
        p <- min
        piv <- A[p]
        i <- min; j <- max
        Do:
            While i < max and A[i] <= piv do:
                i <- i + 1
            End While
            While j >= min and A[j] > piv do:
                j <- j - 1
            End While
            Swap A[i] <-> A[j]
        While i < j
        // Undo last swap when pointers crossed
        Swap A[i] <-> A[j]
        // Place pivot into final sorted position j
        Swap A[p] <-> A[j]
        Return j
    End Function

    If min < max then:
        s <- Partition(A, min, max)
        Quicksort(A, min, s - 1)
        Quicksort(A, s + 1, max)
    End If
End Algorithm

Key Comparison Analysis: Merge Sort vs Quick Sort

  • Merge Sort (Θ(nlog⁡n)\Theta(n \log n) in ALL cases & Bottom-Up Runs): Splitting AA into ALA_L and ARA_R creates two subproblems of size n/2n/2. In the best case (all elements in ALA_L are smaller than all elements in ARA_R), Merge performs ⌊n/2⌋\lfloor n/2 \rfloor key comparisons. In the worst case (interleaved largest and second-largest elements), Merge performs n−1n - 1 comparisons. In both cases, T(n)=2T(n/2)+Θ(n)∈Θ(nlog⁡n)T(n) = 2T(n/2) + \Theta(n) \in \Theta(n \log n) by Master Theorem Case 2. Merge Sort is stable (because Al[i] <= Ar[j] gives priority to the left element) and not in-place (Θ(n)\Theta(n) auxiliary array space). Alternatively, Bottom-Up Merge Sort avoids recursion by treating the input array as nn sorted 1-element subarrays (called runs) and iteratively merging adjacent runs of size 1,2,4,8,…1, 2, 4, 8, \dots.
  • Quick Sort Worst Case (Θ(n2)\Theta(n^2)) vs Average Case (Θ(nlog⁡n)\Theta(n \log n)): When the pivot is chosen as the first element and the array is already sorted (or reverse sorted), every partition splits nn elements into an empty subarray of size 00 and a subarray of size n−1n - 1, degenerating Quick Sort from divide-and-conquer into decrease-by-one: T(n)=T(n−1)+n  ⟹  ∑i=2ni=n(n+1)2−1∈Θ(n2)T(n) = T(n-1) + n \implies \sum_{i=2}^{n} i = \frac{n(n+1)}{2} - 1 \in \Theta(n^2). In the Average Case, random pivot splits require ≈2ln⁡(n)≈1.39log⁡2(n)\approx 2\ln(n) \approx 1.39\log_2(n) recursive levels and Θ(nlog⁡n)\Theta(n \log n) key comparisons.
  • Quick Sort Optimisations (HESS Notes):
    1. Random Pivot Selection or Median-of-Three Partitioning: Choose the median of A[min]A[\text{min}], A[⌊(min+max)/2⌋]A[\lfloor(\text{min}+\text{max})/2\rfloor], and A[max]A[\text{max}] as the pivot (swapping the largest of the three candidates into A[max]), preventing Θ(n2)\Theta(n^2) degeneration on sorted inputs.
    2. Hybrid Insertion Sort Cutoff (QuickAlmostSort + InsertionSort): Stop Quick Sort recursion when subarray size is ≤10\le 10 (lo + 10 < hi), leaving small partitions nearly sorted, and finish with a single pass of InsertionSort over the entire array.
HYBRID QUICK SORT · QUICKALMOSTSORT + INSERTIONSORT CUTOFF HESS OPTIMISATION PSEUDOCODE
Algorithm Sort(A[lo..hi]):
    Function QuickAlmostSort(A[lo..hi]):
        If lo + 10 < hi then:
            s <- Partition(A[lo..hi])
            QuickAlmostSort(A[lo..s - 1])
            QuickAlmostSort(A[s + 1..hi])
        End if
    End Function

    QuickAlmostSort(A[lo..hi])
    InsertionSort(A[lo..hi])
End Algorithm
Self-Check: Solve the recurrence relation T(n)=4T(n/2)+n2T(n) = 4T(n/2) + n^2 and T(n)=3T(n−1)+1T(n) = 3T(n-1) + 1 (T(1)=1T(1) = 1).

1. For T(n)=4T(n/2)+n2T(n) = 4T(n/2) + n^2, apply the Master Theorem with a=4,b=2,k=2a = 4, b = 2, k = 2. Since a=4=22=bka = 4 = 2^2 = b^k (Case 2), T(n)∈Θ(n2log⁡n)T(n) \in \Theta(n^2 \log n).

2. For T(n)=3T(n−1)+1T(n) = 3T(n-1) + 1, the Master Theorem does not apply (subtraction n−1n-1). Using backward substitution: T(n)=3kT(n−k)+∑i=0k−13iT(n) = 3^k T(n-k) + \sum_{i=0}^{k-1} 3^i. Setting k=n−1k = n - 1 yields T(n)=3n−1+3n−1−12=3n−12∈Θ(3n)T(n) = 3^{n-1} + \frac{3^{n-1} - 1}{2} = \frac{3^n - 1}{2} \in \Theta(3^n).