10.07.2015 Views

Divide and Conquer

Divide and Conquer

Divide and Conquer

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Divide</strong> <strong>and</strong> <strong>Conquer</strong> 8item_t a[n]maxmin ( i, j, max, min ){if ( i == j ){max = min = a[i]return}if ( i == j - 1 ){if ( a[i] < a[j] ){max = a[j]min = a[i]}else{max = a[i]min = a[j]}return}// Global array// Only one element// Only two elements// <strong>Divide</strong>mid = floor ( ( i + j ) / 2 )maxmin ( i, mid, max, min )maxmin ( mid+1, j, max1, min1 )// <strong>Conquer</strong>}if ( max < max1 ) max = max1if ( min > min1 ) min = min1return• Analyzing divide <strong>and</strong> conquer maxmin described by the recurrence⎧⎨ T n + T n + 2 n > 22 2T n = 1 n = 2⎩0 n = 1Let n = 2 k , for some k > 0T n = 2T n2 + 2= 2(2T n4 + 2) + 2= 2 2 T n2 2 + 22 + 2= 2 2 (2T n2 3 + 2) + 22 + 2= 2 3 T n2 3 + 23 + 2 2 + 2..= 2 k−1 T n∑k−1+ 2 i2 k−1i=1

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!