数据结构教学课件:Chapter7
1Given a sequence of records R1,R2,Rn withkey values k1,k2,kn,respectively,arrange therecords into any orders such that recordsRs1,Rs2,Rsn have keys obeying the propertyks1 ks2 ksn.Each record contains a field called the key.Linear order:comparison.Measures of cost:Comparisons&SwapsA sorting algorithm is said to stable if it does not change the relative ordering of records with identical key values.23template void inssort(Elem A,int n)for(int i=1;i0)&(Comp:lt(Aj,Aj-1);j-)swap(A,j,j-1);Best Case:0 swaps,n-1 comparisonsWorst Case:n(n-1)/2 swaps and comparisons Average Case:n(n-1)/4 swaps and comparisons456template void bubsort(Elem A,int n)for(int i=0;ii;j-)if(Comp:lt(Aj,Aj-1)swap(A,j,j-1);Best Case:0 swaps,n(n-1)/2 comparisonsWorst Case:n(n-1)/2 swaps and comparisonsAverage Case:n(n-1)/4 swaps and n(n-1)/2 comparisons.78template void selsort(Elem A,int n)for(int i=0;ii;j-)/Find least if(Comp:lt(Aj,Alowindex)lowindex=j;/Put it in place swap(A,i,lowindex);Best Case:0 swaps(n-1 as written),n(n-1)/2 comparisons.Worst Case:n-1 swaps and n(n-1)/2 comparisonsAverage Case:O(n)swaps and n(n-1)/2 comparisons910InsertionBubbleSelectionComparisons:Best Case(n)(n2)(n2)Average Case(n2)(n2)(n2)Worst Case(n2)(n2)(n2)SwapsBest Case00(n)Average Case(n2)(n2)(n)Worst Case(n2)(n2)(n)11Do exercises:Trace by hand the situation of the array(int a=21,25,49,25*,16,08)using Insertion Sort algorithm,Bubble Sort algorithm and Selection sort algorithm respectively.Tell which algorithm is stable or not?1213/Modified version of Insertion Sorttemplate void inssort2(Elem A,int n,int incr)for(int i=incr;i=incr)&(Comp:lt(Aj,Aj-incr);j-=incr)swap(A,j,j-incr);template void shellsort(Elem A,int n)/Shellsort for(int i=n/2;i2;i/=2)/For each incr for(int j=0;ji;j+)/Sort sublists inssort2(&Aj,n-j,i);inssort2(A,n,1);14Do exercises:Trace by hand the situation of the array(int a=49,38,65,97,76,13,27,49*,55,04)using Shellsort(dk=5,3,1).Tell the algorithm is stable or not?15template void qsort(Elem A,int i,int j)if(j=i)return;/List too small int pivotindex=findpivot(A,i,j);swap(A,pivotindex,j);/Put pivot at end /k will be first position on right side int k=partition(A,i-1,j,Aj);swap(A,k,j);/Put pivot in place qsort(A,i,k-1);qsort(A,k+1,j);Divide and Conquer:divide list into values less than pivot and values greater than pivot.Initial call:qsort(array,0,n-1);16template int findpivot(Elem A,int i,int j)return(i+j)/2;template int partition(Elem A,int l,int r,Elem&pivot)do /Move the bounds in until they meet while(Comp:lt(A+l,pivot);while(r!=0)&Comp:gt(A-r,pivot);swap(A,l,r);/Swap out-of-place values while(l r);/Stop when they cross swap(A,l,r);/Reverse last swap return l;/Return first pos on right171819Best case:Always partition in half.(n log n)Worst case:Bad partition.(n2)Average case:T(n)=cn+1/n(T(k)+T(n-1-k)=(n log n)Optimizations for Quicksort:Better Pivot Better algorithm for small sublists Eliminate recursionk=0n-120Do exercises:Trace by hand the execution of Quicksort algorithm on the array:int a=44,77,55,99,66,33,22,88,77*.Tell the algorithm is stable or not?21List mergesort(List inlist)if(inlist.length()=1)return inlist;List l1=half of the items from inlist;List l2=other half of items from inlist;return merge(mergesort(l1),mergesort(l2);22template void mergesort(Elem A,Elem temp,int left,int right)int mid=(left+right)/2;if(left=right)return;mergesort(A,temp,left,mid);mergesort(A,temp,mid+1,right);for(int i=left;i=right;i+)/Copy tempi=Ai;int i1=left;int i2=mid+1;for(int curr=left;curr right)/Right exhausted Acurr=tempi1+;else if(Comp:lt(tempi1,tempi2)Acurr=tempi1+;else Acurr=tempi2+;23template void mergesort(Elem A,Elem temp,int left,int right)if(right-left)=THRESHOLD)inssort(&Aleft,right-left+1);return;int i,j,k,mid=(left+right)/2;if(left=right)return;mergesort(A,temp,left,mid);mergesort(A,temp,mid+1,right);for(i=mid;i=left;i-)tempi=Ai;for(j=1;j=right-mid;j+)tempright-j+1=Aj+mid;for(i=left,j=right,k=left;k=right;k+)if(tempi tempj)Ak=tempi+;else Ak=tempj-;24Mergesort cost:(n log n)in the best,average,and worst cases.Mergsort is also good for sorting linked lists.Mergesort requires twice the space.25Do exercises:Trace by hand the execution of Mergesort algorithm on the array:int a=21,25,49,25*,93,62,72,08,37,16,54.Tell the algorithm is stable or not?26School of Software Engineering,SCUT Longcun Jin(金龙存)(金龙存) Oct.18,201327template void heapsort(Elem A,int n)/Heapsort Elem mval;maxheap H(A,n,n);for(int i=0;in;i+)/Now sort H.removemax(mval);/Put max at endUse a max-heap,so that elements end up sorted within the array.Cost of heapsort:build the heap is(n)remove the n elements is(n log n)Cost of finding K largest elements:(n+k log n)282930Do exercises:Trace by hand the execution of Heapsort algorithm on the array:int a=21,25,49,25*,16,08.Tell the algorithm is stable or not?31A simple,efficient sort:for(i=0;in;i+)BAi=Ai;Ways to generalize:Make each bin the head of a list.Allow more keys than records.32template void binsort(Elem A,int n)List BMaxKeyValue;Elem item;for(i=0;in;i+)BAi.append(Ai);for(i=0;iMaxKeyValue;i+)for(Bi.setStart();Bi.getValue(item);Bi.next()output(item);Cost:(n+MaxKeyValue).3334template void radix(Elem A,Elem B,int n,int k,int r,int cnt)/cnti stores number of records in bini int j;for(int i=0,rtok=1;ik;i+,rtok*=r)for(j=0;jr;j+)cntj=0;/Count number of records for each bin for(j=0;jn;j+)cnt(Aj/rtok)%r+;/cntj will be last slot of bin j.for(j=1;j=0;j-)B-cnt(Aj/rtok)%r=Aj;for(j=0;jn;j+)Aj=Bj;3536Cost:(nk+rk)How do n,k,and r relate?If key range is small,then this can be(n).If there are n distinct keys,then the length of a key must be at least logrn.Thus,Radix Sort is(n logn)in general case37Do exercises:Trace by hand the execution of Radix Sort algorithm on the array:int a=32,13,27,32*,19,33.Tell the algorithm is stable or not?383940We would like to know a lower bound for all possible sorting algorithms.Sorting is O(n log n)(average,worst cases)because we know of algorithms with this upper bound.We will now prove(n log n)lower bound for sorting.4142 There are n!permutations.A sorting algorithm can be viewed as determining which permutation has been input.Each leaf node of the decision tree corresponds to one permutation.A tree with n nodes has(log n)levels,so the tree with n!leaves has(log n!)=(n log n)levels.