Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithm Design & Analysis

Similar presentations


Presentation on theme: "Algorithm Design & Analysis"— Presentation transcript:

1 Algorithm Design & Analysis
Chapter 4 : Mergesort Quicksort Counting sort

2 Divide and Conquer Paradigm
08/11/1439 Divide and Conquer Paradigm 2 2 Prepared by Dr. Zakir H. Ahmed

3 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].

4 08/11/1439 Illustration 4 4 Prepared by Dr. Zakir H. Ahmed

5 08/11/1439 Illustration (Contd.) 5 5 Prepared by Dr. Zakir H. Ahmed

6 08/11/1439 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 6 Prepared by Dr. Zakir H. Ahmed

7 08/11/1439 Illustration 7 7 Prepared by Dr. Zakir H. Ahmed

8 08/11/1439 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 8 Prepared by Dr. Zakir H. Ahmed

9 08/11/1439 Analysis (Contd.) 9 9 Prepared by Dr. Zakir H. Ahmed

10

11

12

13

14

15

16

17

18

19

20


Download ppt "Algorithm Design & Analysis"

Similar presentations


Ads by Google