1CISC320, F05, Lec4, Liao 1CISC 320 Introduction to AlgorithmsFall 2005Lecture 4QuickSort & MergeSortCISC320, F05, Lec4, Liao2 Terminologies Comparison-based sorting In-place Stable sorting2CISC320, F05, Lec4, Liao3Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.CISC320, F05, Lec4, Liao4Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.3CISC320, F05, Lec4, Liao5Insertion Sort: complexity analysis Worst-caseTwc(n) ≤ Σi=1to n-1i = n(n-1)/2for loop to see whether A[i] needs to move while loop to move A[i] at most i times to the front of the arrayCISC320, F05, Lec4, Liao6Why we save? Because elements in range [p, q-1] and range [q+1, r] will never be compared in this divide-and-conquer algorithm.4CISC320, F05, Lec4, Liao7Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.Given any pivot element A[r], it takes n-1 comparisons to partition array A into two subarrays and the right location for A[r] such that all smaller keys are to its left and larger keys to the right. The partition, as shown below, is “in-place”. Is it also stable?CISC320, F05, Lec4, Liao8Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.5CISC320, F05, Lec4, Liao9Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.CISC320, F05, Lec4, Liao10Quicksort: ideal-caseEach call to the partition subroutine will return a splitPoint which is right at the middle of the range, namely, divide the range into two equal subranges.In doing so, at most n comparisons are needed to ensure one subrangecontains only keys that are smaller than the pivot, and the other subrangeonly keys larger than the pivot.Therefore,T(n) = 2T(n/2) + n n/2 n/2n/4 n/4 n/4 n/4nnnlg(n)…n lg(n)…6CISC320, F05, Lec4, Liao11Quicksort: worst-caseEach call to the partition subroutine will return a splitPoint which is just the left boundary of the range, namely, no keys are smaller than the pivot.How many comparisons needed to know this? Still, all elements in the range need to compare with the pivot.When does this happen? Ironically when the array is a sorted one.In this case, divide-and-conquer decays into chip-and-conquer as the 2nd recursive call is quickSort(E, splitPoint + 1, last), chipping off one element from the range.Therefore,T(n) = T(n-1) + n 1 n-1 1 1 n-2 nn-1n-2n…Θ (n2)…CISC320, F05, Lec4, Liao12Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.7CISC320, F05, Lec4, Liao13Quicksort: Average-caseAssumption: Prob(splitPoint = i| splitPoint = partiton[1 to n]) = 1/nThenT(n) = n-1 + ∑i=0 to n-1 (1/n) [T(i) + T(n-1-i)] = n-1 + (1/n) ∑i=1 to n-1 [T(i) + T(n-1-i)] = n-1 + (2/n) ∑i=1 to n-1 T(i) Claim: T(n) ≤ c n lg(n) holds for any n ≥1, where c is a constant.Proof: induction on n.Base case n = 1. One single element array is sorted, i.e., T(1) = 0. As c 1 ln(1) = 0, the theorem holds for the base case.Assume for any n, T(n) ≤ c n lg(n), then T(n+1) = (n+1) -1 + (2/(n+1)) ∑i=1 to n T(i) ≤ n + (2/(n+1) ∑i=1 to n c i lg(i) ≤ n + (2c/(n+1) ∫1n+1 x lg(x) dx= n + 2c [(1/2)(n+1)2lg (n+1) – (1/4) (n+1)2+ (1/4)] / (n+1)= c(n+1) ln(n+1) + (n+1) [ 1- (c/2 ) + (c/2) (n+1) -2]-1 ≤ 2(n+1) ln(n+1) if c = 2 QEDCISC320, F05, Lec4, Liao14Using a randomly chosen element as pivot will enforce the equal distribution assumption made at the average-case analysis.8CISC320, F05, Lec4, Liao15Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.Quicksort: space complexityQuicksort is “in-place” sort, because no extra array is needed. Yet there are hidden space usage: stacks for recursive calls -> Θ(n) for worst case, and lg(n) on average. The following modified algorithm guarantees this lg(n) usage of space.CISC320, F05, Lec4, Liao16Mergesort Unlike quickSort, mergeSort guarantees equal division each time. Array with just a single element is already sorted! Work is done at the combining steps9CISC320, F05, Lec4, Liao17Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.CISC320, F05, Lec4, Liao18Mergesort: example524613271223456724561237254613275246132752461327splitsplitsplitsplit splitsplitsplitmerge merge merge mergemerge mergemerge52461327Is it a stable sorting?10CISC320, F05, Lec4, Liao19Mergesort: analysisWorst-case (Q: when?; and when is best case)T(n) = T(└n/2┘) + T(┌n/2┐) + n -1T(1) = 0Master Theorem => T(n) ∈Θ(n log n)Note: 1stalgorithm by far does nlog(n) for worst-case.Can we do better? Or will be better off on
View Full Document