1、Insertion Sort,Algorithm : Design & Analysis 4,In the last class,Recursive Procedures Proving Correctness of Recursive Procedures Deriving recurrence equations Solution of the Recurrence equations Guess and proving Recursion tree Master theorem Divide-and-conquer,Insertion Sort,Comparison-based sort
2、ing Insertion sort Analysis of insertion sorting algorithm Lower bound of local comparison based sorting algorithm Shell sort,Why Sorting,Practical use Enough different algorithms for comparing Easy to derive the strong lower bounds for worst case and average behavior,Comparison-Based Algorithm,The
3、class of “algorithms that sort by comparison of keys” comparing (and, perhaps, copying) the key no other operations are allowed The measure of work used for analysis is the number of comparison.,As Simple as Inserting,Shifting Vacancy: the Specification,int shiftVac(Element E, int vacant, Key x) Pre
4、condition: vacant is nonnegative Postconditions: Let xLoc be the value returned to the caller, then: Elements in E at indexes less than xLoc are in their original positions and have keys less than or equal to x. Elements in E at positions (xLoc+1, vacant) are greater than x and were shifted up by on
5、e position from their positions when shiftVac was invoked.,Shifting Vacancy: Recursion,int shiftVacRec(Element E, int vacant, Key x) int xLoc 1. if (vacant=0) 2. xLoc=vacant; 3. else if (Evacant-1.keyx) 4. xLoc=vacant; 5. else 6. Evacant=Evacant-1; 7. xLoc=shiftVacRec(E,vacant-1,x); 8. Return xLoc,T
6、he recursive call is working on a smaller range, so terminating; The second argument is non-negative, so precondition holding,Worse case frame stack size is (n),Shifting Vacancy: Iteration,int shiftVac(Element E, int xindex, Key x) int vacant, xLoc; vacant=xindex; xLoc=0; /Assume failure while (vaca
7、nt0) if (Evacant-1.keyx) xLoc=vacant; /Succeed break; Evacant=Evacant-1; vacant-; /Keep Looking return xLoc,Insertion Sorting: the Algorithm,Input: E(array), n0(size of E) Output: E, ordered nondecreasingly by keys Procedure: void insertSort(Element E, int n) int xindex; for (xindex=1; xindexn; xind
8、ex+) Element current=Exindex; Key x=current.key; int xLoc=shiftVac(E,xindex,x); ExLoc=current; return;,Worst-Case Analysis,At the beginning, there are n-1 entries in the unsorted segment, so:,The input for which the upper bound is reached does exist, so: W(n)(n2),Average Behavior,Assumptions: All pe
9、rmutations of the keys are equally likely as input. There are not different entries with the same keys. Note: For the (i+1)th interval (leftmost), only i comparisons are needed.,x may be located in any one of the i+1 intervals(inclusive), assumingly, with the same probabiliy,Sorted (i entries),x,Ave
10、rage Complexity,The average number of comparisons in shiftVac to find the location for the ith element:For all n-1 insertions:,for the leftmost interval,Inversion and Sorting,An unsorted sequence E: x1, x2, x3, , xn-1, xn If there are no same keys, for the purpose of sorting, it is a reasonable assu
11、mption that x1, x2, x3, , xn-1, xn=1,2,3,n-1,nis an inversion if xixj, but ij All the inversions must be eliminated during the process of sorting,Eliminating Inverses: Worst Case,Local comparison is done between two adjacent elements. At most one inversion is removed by a local comparison. There do
12、exist inputs with n(n-1)/2 inversions, such as (n,n-1,3,2,1) The worst-case behavior of any sorting algorithm that remove at most one inversion per key comparison must in (n2),Elininating Inverses: Average,Computing the average number of inversions in inputs of size n (n1): Transpose: x1, x2, x3, ,
13、xn-1, xnxn, xn-1, , x3, x2, x1 For any i, j, (1jin), the inversion (i,j ) is in exactly one sequence in a transpose pair. The number of inversions (i,j ) is n(n-1)/2. So, the average number of inversions in all possible inputs is n(n-1)/4. The average behavior of any sorting algorithm that remove at
14、 most one inversion per key comparison must in (n2),Traveling a Long Way,Problem If a1, a2, an is a random permutation of 1,2,n, what is the average value of| a1-1|+| a2-2|+| a1-n| The answer is the average net distance traveled by all records during a sorting process.,Shellsort: the Strategy,5,8,24
15、,13,7,18,31,19,44,63,82,29,5,7,18,13,8,24,31,19,29,63,82,44,h=6,h=4,h=3,h=2,Shellsort: the Strategy (cont.),For each subsequence, insertion sorting is use, since, generally, in most cycle, there are not many element out of the order.,h=1,The solution,Shellsort: the Algorithm,Input: E, an unsorted ar
16、ray of elements, n0, the number of elements, a sequence of diminishing increments ht, ht-1, h1, where h1=1, and t, the number of increments. Output: E, with the elements in nondecreasing order of their keys. Procedure: void shellSort(Element E, int n, int h, int t);int xindex, s;for (s=t; s1; s-)for
17、 (xindex=hs; xindexn; xindex+)element current=Exindex;key x= current.key;int xLoc=shiftVacH(E, hs, xindex,x);ExLoc=current;return;,A special version for Shellsort,Is there any possibility that Shellsort is an improvement on Insertion Sort, since only the last pass has to do the same thing as Inserti
18、on Sort, let along the passes done before that?,?,Complexity of Shellsort,The efficiency of Shellsort should be higher than insertSort, since: More than 1 inverse may be removed by one comparison, when h1 Sorting with one increment will not undo any of the work done previously when a different incre
19、ment was used The behavior of Shellsort is mostly unknown Function of the sequence of increments used Influences of “pre-processing”(when h1) is unknown,Some Results about Shellsort,If an h-ordered array is k-sorted, the array will still be h-ordered. For a two-pass case of Shellsort(i.e. the increments are 1,h) The average number of inversions in an h-ordered permutation of 1,2,n isRunning time is approximately proportional to the best choice of h is about ,which gives O(n5/3),Home Assignment,pp.208- 4.6 4.8 4.9 4.11 4.45,