A divide-and-conquer algorithm handles small base cases directly, divides a larger instance into smaller instances of the same problem, solves them recursively, and combines their answers. The savings come from the number and sizes of recursive calls and the combination cost.
For Karatsuba multiplication, split the -bit inputs as , , where and . Compute only three recursive products:
Then the Karatsuba three-product identity gives
The absolute differences have at most bits; multiply their magnitudes and restore the sign of . Additions, subtractions and shifts cost bit operations. With constant-size base cases, the recurrence relation is , giving by the Master theorem. The difference-product version avoids an extra carry bit in the recursive operands.
Use minimum search in a rotated distinct array, then return the cyclic predecessor of the minimum:
rotatedMaximum(A):
    n = length(A)                 # require n >= 1
    lo = 0
    hi = n - 1
    while lo < hi:
        mid = floor((lo + hi) / 2)
        if A[mid] > A[hi]:
            lo = mid + 1
        else:
            hi = mid
    return A[(lo + n - 1) mod n]
The loop invariant says that the minimum index lies in the inclusive interval. If A[mid] > A[hi], the rotation break lies to the right of mid; otherwise the segment from mid to hi is increasing and its indices after mid cannot be the minimum. Each update at least halves the interval. Distinct entries make the comparison decisive, including a completely unshifted array. The predecessor of the unique minimum is the maximum, and the one-element case returns that element. The running time is , with auxiliary space.
For fixed , and , a version of the Master theorem states that, with bounded positive base costs,
Usual floors and ceilings preserve these bounds when a sufficiently large constant-size base case ensures strict size decrease.
  • : , hence , with exponent approximately . Base cases must include because the ceiling otherwise need not decrease.
  • : , hence by comparison with the equality recurrence having overhead. If that overhead is actually quadratic this is also tight; the stated upper bound alone does not establish a matching lower bound.
  • : , whose expansion is a geometric series, hence , in fact for nonzero base costs. The divide-by-a-constant Master theorem does not apply to a decrement by one.
is asymptotically fastest; is slowest. Even the quadratic-logarithmic upper bound for is smaller than the tight polynomial order of .

Articles by others on the same topic (0)

There are currently no matching articles.