15. auxiliary array MERGE A G H I L M O R first half exhausted S T smallest A G L O R H I M S T
16. auxiliary array MERGE A G H I L M O R first half exhausted S T second half exhausted A G L O R H I M S T
17. MERGE ( A, p,q, r ) n 1 q-p+1 n 2 r-q create arrays L[1..n 1 +1] and R[1.. n 2 +1] for i 1 to n 1 do L[i] A[p+i-1] for j 1 to n 2 do R[j] A[q+j] L[n 1 +1] R[n 2 +1] i 1 j 1 for k p to r do if L[i] R[j] then A[k] L[i] i i+1 else A[k] R[j] j j+1 end_if end_for end. MERGE
18. ... 2 7 MERGE ( A, 5,8,11 ) 9 A 5 1 7 9 k 2 6 4 7 8 ... 9 10 11 12 3 5 8 1 8 5 3 L R i j
19. ... 2 7 9 A 5 7 9 2 6 4 7 8 ... 9 10 11 12 3 5 8 1 8 5 3 L R 1 k i j
20. ... 3 9 A 5 7 9 2 6 4 7 8 ... 9 10 11 12 3 5 8 1 8 5 3 L R 1 2 k i j
21. ... 5 A 5 7 9 2 6 4 7 8 ... 9 10 11 12 3 5 8 1 8 5 3 L R 1 2 3 k i j
22. ... A 5 7 9 2 6 4 7 8 ... 9 10 11 12 7 5 8 1 8 5 3 L R 1 2 3 5 k i j
23. ... A 5 7 9 2 6 4 7 8 ... 9 10 11 12 8 8 1 8 5 3 L R 1 2 3 5 7 k i j
24. ... A 5 7 9 2 6 4 7 8 ... 9 10 11 12 8 1 8 5 3 L R 1 2 3 5 7 8 k i j
25. ... A 5 7 9 2 6 4 7 8 ... 9 10 11 12 1 8 5 3 L R 1 2 3 5 7 8 9 k i j
26. MERGE- Correctness Loop Invariant At the start of each iteration of the for loop for k, the sub-array A[p,..,k-1] consist of the k-p smallest elements of L[1,.., n 1 +1] and R[1,.., n 2 +1] in sorted order. Moreover L[i] and R[j] are the smallest of their arrays that have not been copied back into A.
27. Before the beginning of the loop k=p, then the sub-array A[p,..,k-1] is empty and consist of the k-p = 0 smallest elements of L[1,.., n 1 +1] and R[1,.., n 2 +1]. Since i=j=1 then L[1] and R[1] are the smallest of their arrays that have not been copied back into A. INITILIZATION
28. MAINTENANCE Before the beginning of the l-th iteration of the loop k=p+l, then the sub-array A[p,..,k-1] consist of the (k-p = l) smallest elements of L[1,.., n 1 +1] and R[1,..,n 2 +1] in sorted order and L[i] and R[j] are the smallest elements of their arrays that have not been copied back into A.
29. Let us first assume that L[i] R[j] then following the loop L[i] is copied to A[k =p+l] therefore before the beginning of the (l+1)th k=p+l+1 and A[p,..,k-1] = A[p,..,p+l] consist of the (l+1) smallest elements of L[1,.., n 1 +1] and R[1,..,n 2 +1] and L[i] and R[j] are the smallest elements of their arrays that have not been copied back into A.
30. Now suppose that R[j] < L[i] then following the loop R[j] is copied to A[k =p+l] therefore before the beginning of the (l+1)th k=p+l+1 and A[p,..,k-1] = A[p,..,p+l] consist of the (l+1) smallest elements of L[1,..,n 1 +1] and R[1,..,n 2 +1] and L[i] and R[j] are the smallest elements of their arrays that have not been copied back into A.
31. At termination k=r+1, then the sub-array A[p,..,k-1]= A[p,..,r] consist of the r smallest elements of L[1,..,n 1 +1] and R[1,..,n 2 +1] in sorted order. Since i= n 1 +1 and j= n 2 +1 then L[i] and R[j] are . TERMINATION
32. MERGE-SORT MERGE(A, p, r): procedure that takes time (n), where n=r-p+1 To sort call MERGE(A, 1, length[A]) with A = [ a 1 ,a 2 ,a 3 ,...,a n ] procedure MERGE-SORT( A, p, r ) if p<r then q (p+r)/2 MERGE-SORT( A, p, q ) MERGE-SORT( A, q+1, r ) MERGE ( A, p, q, r )
34. Time complexity Analyzing divide and conquer algorithms The time can often be described by recurrence equation of the form (1), if n c, T(n) = aT(n/b)+D(n)+C(n) if n>c With a,b and c be nonnegative constants. If the problem is the small enough, say n c , then the solution takes constant time (1). If not the problem is divided in a subproblems with (1/b) size of the original. The division takes time D(n) and the combinations of sub-solutions takes time C(n) .
35. Analyzing MERGE-SORT In this case a=2, b=2, c=1, D(n)= (1) and C(n)= (n) then (1), if n 1 , T(n) = 2T(n/2)+ (1)+ (n) if n>1 c if n 1 , T(n) = 2T(n/2)+ cn if n>1
37. Proof by Picture of Recursion Tree T( n ) T( n /2) T( n /2) T( n /4) T( n /4) T( n /4) T( n /4) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) cn T( n / 2 i ) c2( n /2) c4( n /4) c2 i ( n / 2 i ) cn . . . . . . lg n+1 cn(1+ lg n)
38. Lets suppose n power of two n=2 k The construction of the recursion tree T( n ) cn T( n /2) T( n /2)
40. cn c(n/2) c(n/2) c( n /4) c( n /4) c( n /4) c( n /4) T( n /8) T( n /8) T( n /8) T( n /8) T( n /8) T( n /8) T( n /8) T( n /8)
41. cn c(n/2) c(n/2) c( n /4) c( n /4) c( n /4) c( n /4) c( n /8) c( n /8) c( n /8) c( n /8) c( n /8) c( n /8) c( n /8) c( n /8) c c c c c c c c c c c c c c c c lg n+1 n Total: cn (lg n + 1) cn lg n + cn cn cn cn cn cn
42. k times Lets assume that n is power of two, i.e., n=2 k , k = lg n T(n) = 2T(n/2)+ cn = 2[2T(n/4)+ cn/2]+ cn = 4T(n/4)+ cn+ cn = 4[2T(n/8)+cn/4]+ cn+ cn= 8T(n/8)+cn+ cn+ cn . . = 2 k T(n/2 k )+cn+ . . . +cn . . = 2 k T(1)+k(cn) = cn+cn lg n Recursive substitution