200 likes | 387 Views
Algorithm Design & Analysis. Chapter 4 : Mergesort Quicksort Counting sort. Divide and Conquer Paradigm. 2. Algorithm Merge(). Algorithm Merge(A, p, q, r) { n1 := q – p + 1; n2 := r – q; for i := 1 to n1 do L[i] := A[p+i-1]; for j := 1 to n2 do
E N D
Algorithm Design & Analysis Chapter 4 : Mergesort Quicksort Counting sort
Algorithm Merge() Algorithm Merge(A, p, q, r) { n1 := q – p + 1; n2 := r – q; for i := 1 to n1 do L[i] := A[p+i-1]; for j := 1 to n2 do R[j] := A[q+j]; L[n1+1] := ∞; R[n2+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;} } • The procedure assumes that the subarrays A[p..q] and A[q+1..r] are in sorted order. • It merges them to form a single sorted subarray that replaces the current subarray A[p..r].
Merge Sort • The running time of Merge() is is Θ(n), where n = r – p + 1. • We can now use the Merge procedure as a subroutine in the merge sort algorithm. • The procedure MergeSort(A, p, r) sorts the elements in the subarray A[p..q]. • If p ≥ r, the subarray has at most one element and is therefore already sorted. • Otherwise, the divide step simply computes an index q that partitions A[p..r] into two subarrays: A[p..q], containing elements, and A[q+1..r], containing elements. Algorithm MergeSort(A, p, r) { if (p < r) then { q := (p+r)/2; //floor of (p+r)/2 MergeSort(A, p, q); MergeSort(A, q+1, r); Merge(A, p, q, r); } } 6
Analysis of Merge Sort • Merge sort on just one element takes constant time. When we have n > 1 elements, we break down the running time as follows: • Divide: The divide step just compute the middle of the subarray, which takes constant time. Thus D(n)= Θ (1). • Conquer: We recursively solve two sub-problems, each of size n/2, which contributes 2T(n/2) to the running time. • Combine: We have already noted that Merge procedure on an n-element sub-array takes time Θ(n), so C(n)= Θ(n). • Hence • By using Master Theorem, we get T(n)=Θ(n lg n). • We can prove by recursion-tree method also. • For that let us rewrite the recurrence as follows: 8