SUNY Geneseo Department of Computer Science
Tuesday, February 19
CSci 242, Spring 2013
Prof. Doug Baldwin
Quicksort
quicksort( A, first, last )
if first > last
p = partition( A, first, last ) // Θ(n)
quicksort( A, first, p-1 ) // T(n/2)
quicksort( A, p+1, last ) // T(n/2)
partition
runs in Θ(n) time, and divides A exactly in halfStrassen’s algorithm
strassen( A, B, n )
if n == 1
return A[1][1] * B[1][1]
else
compute S1 through S10, sums and differences of n/2-by-n/2 matrices
compute P1 through P7, products of n/2-by-n/2 matrices
compute C as sums and differences of certain P matrices
return C
given k-by-k matrices X and Y, with sum to go in Z
for i = 1 to k
for j = 1 to k
Z[i][j] = X[i][j] + Y[i][j]
Our kth-smallest algorithm
kth( k, A, first, last )
if first == last
return A[first]
else
p = partition( A, first, last )
if k < p - first + 1
return kth( k, A, first, p-1 )
else if k > p - first + 1
return kth( k - p + first - 1, A, p+1, last )
else
return A[p]
kth
, so the recurrence only has 1 T(n/2) in it even though 2 appear textually in the algorithmNaive version of exponentiation
power( x, n )
if n == 0
return 1
else if n is even
return power( x, n/2 ) * power( x, n/2 )
else
return x * power(x,(n-1)/2) * power(x,(n-1)/2)
power
more than needed) turned the Θ(logn) time calculated last class into something exponentially worse, and no better than the brute-force algorithm that multiplies n copies of x togetherHand out master method problem set
Introduction to dynamic programming
Read chapter 15 introduction and section 15.1