1、Quicksort,Algorithm : Design & Analysis 5,In the last class,Comparison-based sorting Insertion sort Analysis of insertion sorting algorithm Lower bound of local comparison based sorting algorithm Shellsort,Quicksort,General pattern of divide-and conquer Quicksort: the Strategy Quicksort: the Algorit
2、hm Analysis of Quicksort Improvements of the Algorithm,“What a wonderful way to start a career in Computing, by discovering a new sorting algorithm.“,- C.A.R. Hoare,Quicksort: the Strategy,Dividing the array to be sorted into two parts: “small” and “large”, which will be sorted recursively.,first,la
3、st,for any element in this segment, the key is less than pivot.,for any element in this segment, the key is not less than pivot.,QuickSort: the algorithm,Input: Array E and indexes first, and last, such that elements Ei are defined for firstilast. Output: Efirst,Elast is a sorted rearrangement of th
4、e same elements. The procedure:void quickSort(Element E, int first, int last)if (firstlast)Element pivotElement=Efirst;Key pivot=pivotElement.key;int splitPoint=partition(E, pivot, first, last);EsplitPoint=pivotElement;quickSort(E, first, splitPoint-1);quickSort(E, splitPoint+1, last);return,The spl
5、itting point is chosen arbitrariry, as the first element in the array segment here.,Partition: the Strategy,Partition: the Process,Always keep a vacancy before completion.,First met key that is less than pivot,First met key that is larger than pivot,Moving as far as possible!,Vacant left after movin
6、g,highVac,lowVac,Partition: the Algorithm,Input: Array E, pivot, the key around which to partition, and indexes first, and last, such that elements Ei are defined for first+1ilast and Efirst is vacant. It is assumed that firstlast. Output: Returning splitPoint, the elements origingally in first+1,la
7、st are rearranged into two subranges, such that the keys of Efirst, , EsplitPoint-1 are less than pivot, and the keys of EsplitPoint+1, , Elast are not less than pivot, and firstsplitPointlast, and EsplitPoint is vacant.,Partition: the Procedure,int partition(Element E, Key pivot, int first, int las
8、t)int low, high; 1. low=first; high=last; 2. while (lowhigh) 3. int highVac=extendLargeRegion(E,pivot,low,high); 4. int lowVac = extendSmallRegion(E,pivot,low+1,highVac); 5. low=lowVac; high=highVac-1; 6 return low; /This is the splitPoint,highVac has been filled now.,Extending Regions,Specification
9、 for Precondition: lowVachigh Postcondition: If there are elements in ElowVac+1,.,Ehigh whose key is less than pivot, then the rightmost of them is moved to ElowVac, and its original index is returned. If there is no such element, lowVac is returned.,extendLargeRegion(Element E, Key pivot, int lowVa
10、c, int high),Example of Quicksort,45 14 62 51 75 96 33 84 20,45 as pivot,20 14 62 51 75 96 33 84,20 14 51 75 96 33 84 62,high,highVac,low,lowVac,20 14 51 75 96 33 84 62,low,high =highVac-1,20 14 33 51 75 96 84 62,highVac,20 14 33 75 96 51 84 62,highVac,lowVac,To be processed in the next loop,Divide
11、and Conquer: General Pattern,solve(I)n=size(I);if (nsmallSize)solution=directlySolve(I)elsedivide I into I1, Ik;for each i1,kSi=solve(Ii);solution=combine(S1 , ,Sk);return solution,T(n)=D(n)+ T(size(Ii)+C(n)for nsmallSize,T(n)=B(n) for nsmallSize,Workhorse,“Hard division, easy combination” “Easy div
12、ision, hard combination”Usually, the “real work” is in one part.,Worst Case: a Paradox,For a range of k positions, k-1 keys are compared with the pivot(one is vacant). If the pivot is the smallest, than the “large” segment has all the remaining k-1 elements, and the “small” segment is empty. If the
13、elements in the array to be sorted has already in ascending order(the Goal), then the number of comparison that Partition has to do is:,Average Analysis,Assumption: all permutation of the keys are equally likely. A(n) is the average number of key comparison done for range of size n. In the first cyc
14、le of Partition, n-1 comparisons are done If split point is Ei(each i has probability 1/n), Partition is to be executed recursively on the subrange 0,i and i+1,n-1,The Recurrence Equation,with i0,1,2,n-1, each value with the probability 1/nSo, the average number of key comparison A(n) is:and A(1)=A(
15、0)=0,The number of key comparison in the first cycle(finding the splitPoint) is n-1,Why the assumed probability is still hold for each subrange?,No two keys within a subrange have been compared each other!,Simplified Recurrence Equation,Note: So: Two approaches to solve the equation Guess and prove
16、by induction Solve directly,Guess the Solution,A special case as clue for guess Assuming that Partition divide the problem range into 2 subranges of about the same size. So, the number of comparison Q(n) satisfy: Q(n) n+2Q(n/2) Applying Master Theorem, case 2: Q(n)(nlogn)Note: here, b=c=2, so E=lg(b
17、)/lg(c)=1, and, f(n)=n=nE,Inductive Proof: A(n)O(nlnn),Theorem: A(n)cnlnn for some constant c, with A(n) defined by the recurrence equation above. Proof: By induction on n, the number of elements to be sorted. Base case(n=1) is trivial. Inductive assumption: A(i)cilni for 1in,For Your Reference,Harm
18、onic Series,Inductive Proof: A(n)(nlnn),Theorem: A(n)cnlnn for some co c, with large n Inductive reasoning:,Inductive assumption,Directly Derived Recurrence Equation,Combining the 2 equations in some way, we can remove all A(i) for i=1,2,n-2,Solving the Equation,We have equation:,Let it be B(n),Note
19、: lnn 0.693 lgn,Space Complexity,Good news: Partition is in-place Bad news: In the worst case, the depth of recursion will be n-1 So, the largest size of the recursion stack will be in (n),Recursion on Small Partition,void quickSort(Element E, int first, int last)int first1, last1, first2, last2;fir
20、st2=first; last2=last;while (last2-first21)pivotElement=Efirst; pivot=pivotElement.key;int splitPoint=partition(E,pivot,first2,last2)Esplitpoint=pivotElement;If (splitPoint(first2+last2)/2)first1=first2; last1=splitPoint-1; first2=splitPoint+1; last2=last2elsefirst1=splitPoint+1; last1=last2; first2
21、=first2; last2=splitPoint-1;quickSortTRO(E, first1, last1);return,Recursion:small partition,iteration: larger partition,Eliminating Recursion,void quickSort(Element E,int first, last)int stack; int top;while truewhile (last-first1)pivotElement=Efirst;pivot=pivotElement.key;int splitPoint=partition(E
22、,pivot,first,last);if (splitPoint(first+last)/2)stacktop+=splitPoint+1; stacktop+=last;first=first; last=splitPoint-1;elsestacktop+=first; stacktop+=splitPoint-1;first=splitPoint+1; last=last;if top=0 return;first=stacktop; last=stacktop-1; top=top-2;,processing in inner loop determines the stack depth,Dealing with Duplicated Keys,Three-way comparison,splitpoint,Elements with keys equal to the pivot,Home Assignment,pp.209- 4.12 4.17 4.18 4.21 4.22,