ch4.notes - Sorting list stored as array sort on key that...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Sorting list stored as array sort on key that uniquely represents a record count comparisons for work determine if inplace or not! INSERTION SORT or push down sort - Worst case for each insert max comps are i-1 w(n) = sum i = 2 to n (i - 1) = [n(n-1)]/2 O(n ** 2) - Average case all keys unique all permutations of keys are equally likely i positions where x might go 1/i chance that the number belongs in any position so work for one insertion sum j = 1 to i-1 1/i * j + 1/i * (i - 1) = (i + 1) / 2 - 1/i so amount of total work for all insertions a(n) = sum i = 2 to n of above . .. An in place sort - Lower Bound on the behavior of Certain Sorting Algorithms Let the basic operation be: compare adjacent keys and swap if necessary Show all algorithms that do a limited swapping must do a certain amount of work! There are n! permutations on n items There is one permutation for which the list is sorted. The original list of keys is a permutation. inversion 2, 4, 1, 5, 3 has inversions (2,1), (4,1), (5,3),(4,3) Sorting algorithm removes at most one inversion after each key comparison. So the number of comparisons performed on the input is at least the number of inversions. So consider inversions. There is a permutation that has n(n-1)/2 inversions (5,4,3,2,1) So worst case behavior must be omega(n ** 2) Lower bound worst case is this, can't be done in fewer comparisons or less work. Consider the average lower bound. Done by considering the average number of inversions. Each permutation has a transpose. There is one inversion for every pair of numbers either in the transpose or in the original permutation.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
inversions. So the best we can do is about n**2 /4. The lower bound! Section 2.3 Quicksort next!
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/10/2011 for the course COT 4400 taught by Professor Eggen,r during the Fall '08 term at UNF.

Page1 / 11

ch4.notes - Sorting list stored as array sort on key that...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online