Presentation is loading. Please wait.

Presentation is loading. Please wait.

Insertion Sort Algorithm : Design & Analysis [4].

Similar presentations


Presentation on theme: "Insertion Sort Algorithm : Design & Analysis [4]."— Presentation transcript:

1 Insertion Sort Algorithm : Design & Analysis [4]

2 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

3 Insertion Sort Comparison-based sorting Insertion sort Analysis of insertion sorting algorithm Lower bound of local comparison based sorting algorithm Shell sort

4 Why Sorting Practical use Enough different algorithms for comparing Easy to derive the strong lower bounds for worst case and average behavior

5 Comparison-Based Algorithm The 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.

6 As Simple as Inserting

7 Shifting Vacancy: the Specification int shiftVac(Element[ ] E, int vacant, Key x) Precondition: 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 one position from their positions when shiftVac was invoked.

8 Shifting Vacancy: Recursion int shiftVacRec(Element[] E, int vacant, Key x) int xLoc 1. if (vacant==0) 2. xLoc=vacant; 3. else if (E[vacant-1].key  x) 4. xLoc=vacant; 5. else 6. E[vacant]=E[vacant-1]; 7. xLoc=shiftVacRec(E,vacant-1,x); 8. Return xLoc The 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)

9 Shifting Vacancy: Iteration int shiftVac(Element[] E, int xindex, Key x) int vacant, xLoc; vacant=xindex; xLoc=0; //Assume failure while (vacant>0) if (E[vacant-1].key  x) xLoc=vacant; //Succeed break; E[vacant]=E[vacant-1]; vacant--; //Keep Looking return xLoc

10 Insertion Sorting: the Algorithm Input: E(array), n  0(size of E) Output: E, ordered nondecreasingly by keys Procedure: void insertSort(Element[] E, int n) int xindex; for (xindex=1; xindex<n; xindex++) Element current=E[xindex]; Key x=current.key; int xLoc=shiftVac(E,xindex,x); E[xLoc]=current; return;

11 Worst-Case Analysis At the beginning, there are n-1 entries in the unsorted segment, so: To find the right position for x in the sorted segment, i comparisons must be done in the worst case. Sorted (i entries) x The input for which the upper bound is reached does exist, so: W(n)  (n 2 )

12 Average Behavior Assumptions: All permutations 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

13 Average 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

14 Inversion and Sorting An unsorted sequence E: x 1, x 2, x 3, …, x n-1, x n If there are no same keys, for the purpose of sorting, it is a reasonable assumption that {x 1, x 2, x 3, …, x n-1, x n }={1,2,3,…,n-1,n} is an inversion if x i >x j, but i<j All the inversions must be eliminated during the process of sorting

15 Eliminating Inverses: Worst Case Local comparison is done between two adjacent elements. At most one inversion is removed by a local comparison. There do 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  (n 2 )

16 Elininating Inverses: Average Computing the average number of inversions in inputs of size n (n>1): Transpose: x 1, x 2, x 3, …, x n-1, x n x n, x n-1, …, x 3, x 2, x 1 For any i, j, (1  j  i  n), 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 most one inversion per key comparison must in  (n 2 )

17 Traveling a Long Way Problem If a 1, a 2, …a n is a random permutation of {1,2,…n}, what is the average value of | a 1 -1|+| a 2 -2|+…+| a 1 -n| The answer is the average net distance traveled by all records during a sorting process.

18 Shellsort: the Strategy 7 192413318 82 184463529 7 182413 5 88219 44 633129 5 824 13 718 31 1944 63 8229 5 7 18 13 8 24 31 19 29 63 82 44 h=6 h=4 h=3 h=2

19 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. 578131819292431448263 h=1 578131819242931446382 The solution

20 Shellsort: the Algorithm Input: E, an unsorted array of elements, n  0, the number of elements, a sequence of diminishing increments h t, h t-1,…, h 1, where h 1 =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; s  1; s--) for (xindex=h[s]; xindex<n; xindex++) element current=E[xindex]; key x= current.key; int xLoc=shiftVacH(E, h[s], xindex,x); E[xLoc]=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 Insertion Sort, let along the passes done before that? ???

21 Complexity of Shellsort The efficiency of Shellsort should be higher than insertSort, since: More than 1 inverse may be removed by one comparison, when h>1 Sorting with one increment will not undo any of the work done previously when a different increment was used The behavior of Shellsort is mostly unknown Function of the sequence of increments used Influences of “pre-processing”(when h>1) is unknown

22 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} is Running time is approximately proportional to the best choice of h is about,which gives O(n 5/3 )

23 Home Assignment pp.208- 4.6 4.8 4.9 4.11 4.45


Download ppt "Insertion Sort Algorithm : Design & Analysis [4]."

Similar presentations


Ads by Google