1. MergeSort
  2. Quick Median
  3. Integer Multiplication classical
  4. Integer Multiplication Karatsuba

       

       

       

       

       


        1. MergeSort

        sort first half, sort second half, merge the two sorted halves.

        T(1) = C,
        T(n) = 2T(n/2) + O(n).

       

        Meaning:
T(1) is some constant, we don't care what, and
T(n) is less than 2T(n/2) + kn, for some constant k, we don't care what.

Somewhat surprisingly this is enough information to usefully bound T(n) --- which we will do later.


    2. Quick Median

        QuickMedian(A,n) = select(A, n, n/2)

        select(A,n,k) {
        // Input: Array A size n > 0, and 0 <= k < n
        // Output: The item that would be A[k] if A were sorted. A may be permuted.
        if (n == 1)
            return A[0]
        Partition A at random entry which ends up in position i.
        //i is now set and A is permuted so that A[1..i] <= A[i] <= A[i..n]
        if (k < i)
            return select(A,i,k)
        else
            return select(A+i,n-i,k-i)
        }

        T(n) = T(9n/10) + O(n) [probably]


    3,4. Integer Multiplication (classical, Karatsuba)

        Input: n bit numbers x, y
        Output: the 2n bit product z = xy

        3. Classical algorithm, written recursively:
        Extract upper and lower halves
            x = a 2n/2 + b
            y = c 2n/2 + d
        Then xy = ac 2n + (ad + bc)2n/2 + bd

        T(n) = 4T(n/2) + O(n)

    2n             3n/2            n              n/2            0
                              x =  |------a-------|------b--------|
                              y =  |------c-------|------d--------|
                    
xy = |------------ac---------------|--------------bd--------------|
                 + |-----------(ad + bc)----------|

Example in base 10, n = 8.
x = 10012002
y = 30034004
a = 1001 
b = 2002 // x = a*10^4 + b
c = 3003
d = 4004 // y = c*10^4 + d
ac = 03006003
bd = 08016008
ad = 04008004
bc = 06012006
ad+bc = 10020010
xy = |03006003 08016008| // ac * 10^8 + bd
        + 1002 0010      // (ad + bc)*10^4
   = |03007005 08116008|

        xy = ac 2n + ((a + b)(c + d) - ac - bd)2n/2 + bd

        T(n) = 3T(n/2) + O(n)

Example in base 10, n = 8.
x = 10012002
y = 30034004
a = 1001
b = 2002
c = 3003
d = 4004
s1 = a+b = 3003
s2 = c+d = 7007
p1 = ac = 03006003
p2 = bd = 08016008
p3 = s1*s2 = (a+b)*(c+d) = 21042021 // ac + ad + bc + bd
s3 = ac + bd = 11022011
s4 = p3 - s3 = 10020010 // ad + bc
s5 = p1 followed by p2 = 0300600308016008
ans = s5 + s4(shifted) = 03006003 08016008 // ac followed by bd
                           + 1002 0010     // ad + bc appropriatedly shifted
                       = 03007005 08116008

       

Summary: Each divide and conquer algorithm leads to a recurrence formula for the run time

    MergeSort: TM = 2TM(n/2) + O(n)

    Quick Median: TQ = TQ(n/2) + O(n) (probably)

    Integer Multiplication (classical): TC = 4TC(n/2) + O(n)

    Integer Multiplication (Karatsuba): TK = 3TK(n/2) + O(n)