1 / 20

Algorithm Design & Analysis

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

andeana
Download Presentation

Algorithm Design & Analysis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Divide and Conquer Paradigm 2

  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. Illustration 4

  5. Illustration (Contd.) 5

  6. 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

  7. Illustration 7

  8. 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

  9. Analysis (Contd.) 9

More Related