cse101_10_12_11

# cse101_10_12_11 - Return x[0 o merge[1k Y Else return y[0 o...

This preview shows page 1. Sign up to view the full content.

Divide-And-Conquer 1. Extreme recursion To solve a problem P: Create smaller subproblems P 1 , P 2 Solve these recursively Glue together the answers to get an answer for P (specify split/gluing) 2. Mergesort a. To sort an array: Split into two Split each half Merge the two sorted halves b. Function mergesort(A[1. .n]) Input: Array to be sored If n = 1: return A Else: Return merge(mergesort(A[1…[n/2]], mergesort(A[n/2]+1…n))) Function merge(x[1…k], y[1…l]) If (x[0] < y[0]):
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Return x[0] o merge([1k], Y) Else return y[0] o merge(x, y[1l]) c. How long does merge take? On each recursive call: O(1) time Total array size shrinks by 1 Initial array size = k+l Therefore, time = O(k+l), linear d. How long does mergesort take? T(n) = time taken to sort array of size n = 2T(n/2) + O(n) T(n) = 2T(n/2) + cn 2[2T(n/4)+cn/2] + cn = 4T9n/4) + 2cn = 4[2T(n/8) + cn/4] + 2cn = 8T(n/8) + 3cn = O(n log n) e....
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online