H-SC COMS 262 - Lecture 34 - Shell and Merge Sorts

Unformatted text preview:

SortingComparison of Run TimesThe Shell SortThe Merge SortAssignmentShell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentShell and Merge SortsLecture 34Sections 13.1, 13.4Robb T. KoetherHampden-Sydney CollegeMon, Apr 13, 2009Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentOutline1Sorting2Comparison of Run Times3The Shell Sort4The Merge Sort5AssignmentShell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentSortingDefinition (Sort)To sort a list is to arrange the members into eitherincreasing order or decreasing order.Definition (Total Order Relation)A total order relation on a set is a relation that has thefollowing two properties.Transitivity: If a < b and b < c, then a < c.Trichotomy: Either a = b, or a < b, or a > b.The order is determined by an order operator: <, >, <=,or >=.This operator must define a total order on the class.Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentInefficient Sorting AlgorithmsMost elementary sorting algorithms are inefficient forlong lists.ExamplesBubble Sort.Selection Sort.Insertion Sort.These algorithms have run times of order O(n2).Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentEfficient Sorting AlgorithmsThe efficient sorting algorithms are more complicated.ExamplesShell SortMerge SortQuick SortThese algorithms have run times of order O(n log n).Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentComparison of AlgorithmsHow much fast is O(n log n) than O(n2)? Let’s compare.Let A be an algorithm of order O(n2).Let B be an algorithm of order O(n log n).Suppose both algorithms require 1 µsec to process alist of size n = 100.How long will they take to process lists of sizes 103, 104,105, 106, 107, 108, and 109?Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentComparison of AlgorithmsAlgorithm A has run timen21002= 0.0001n2.Algorithm B has run timen log n100 log 100= 0.005n log n.Evaluate these functions when n = 102, 103, 104, 105,106, 107, 108, and 109.Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentComparison of Algorithmsn Algorithm A Algorithm B1021 µs 1 µs103100 µs 15 µs10410 ms 200 µs1051 s 2.5 ms106100 s 30 ms1072.8 h 350 ms10811.6 d 4 s1093.2 y 45 sShell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentThe Shell SortThe Shell Sort is a variation of the Insertion Sort.The run time is O(n log n) with probability near 100%.The worst case has run time O(n2) with probability near0%.Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentThe Shell Sort AlgorithmStart with a “gap” equal to half the size of the list.Perform Insertion Sorts on the interwoven sublists(k = gap).{a0, ak, a2k, . . .}{a1, ak+1, a2k+1, . . .}{a2, ak+2, a2k+2, . . .}...{ak−1, a2k−1, a3k−1, . . .}.Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentThe Shell SortExample (The Shell Sort)Let the list be{30, 60, 80, 20, 90, 50, 10, 70, 40}.Begin with a list of size n = 9.Initialize gap = 4.Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentExample: The Shell SortExample (The Shell Sort)Pass #1, first sublist.30 60 80 20 90 50 10 70 4030 60 80 20 40 50 10 70 900 840 84Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentExample: The Shell SortExample (The Shell Sort)Pass #1, 2nd sublist.30 60 80 20 40 50 10 70 9030 50 80 20 40 60 10 70 901 51 5Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentExample: The Shell SortExample (The Shell Sort)Pass #1, 3rd sublist.30 60 80 20 40 50 10 70 9030 50 10 20 40 60 80 70 902 62 6Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentExample: The Shell SortExample (The Shell Sort)Pass #1, 4th sublist.30 60 80 20 40 50 10 70 9030 50 10 20 40 60 80 70 903 73 7Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentExample: The Shell SortExample (The Shell Sort)Divide gap by 2 and repeat the above procedure.Quit when gap = 0.Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentExample: The Shell SortExample (The Shell Sort)Halve the gap: gap = 4 / 2 = 2.Pass #2, 1st sublist30 60 10 20 40 50 80 70 9010 60 30 20 40 50 80 70 902 60 42 60 4 88Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentExample: The Shell SortExample (The Shell Sort)Halve the gap: gap = 4 / 2 = 2.Pass #2, 2nd sublist30 60 80 20 40 50 10 70 9010 20 30 50 40 60 80 70 903 71 53 71 5Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentExample: The Shell SortExample (The Shell Sort)Halve the gap: gap = 2 / 2 = 1.10 50 30 20 40 60 80 70 9010 20 30 40 50 60 70 80 903 71 53 71 5Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentThe Merge SortMerging two sorted lists of length n has run time O(n).The run time of the Merge Sort is O(n log n).Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentThe Merge Sort AlgorithmBegin by considering the list to be a collection ofsublists each of length 1.50 30 70 20 80 40 10 60Merge adjacent sublists in pairs.Continue to merge adjacent sublists until there remainsonly one sublist.Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentExample: The Merge SortExampleThe Merge SortBegin with a list of size n = 8.50 30 70 20 80 40 10 60Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentExample: The Merge SortExampleThe Merge SortPass #1: Sublist size = 1.30 50 70 20 80 40 10 60Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe MergeSortAssignmentExample: The Merge SortExampleThe Merge SortPass #2: Sublist size = 2.30 50 20 70 80 40 10 60Shell andMerge SortsRobb T.KoetherSortingComparisonof Run TimesThe Shell SortThe


View Full Document

H-SC COMS 262 - Lecture 34 - Shell and Merge Sorts

Download Lecture 34 - Shell and Merge Sorts
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 34 - Shell and Merge Sorts and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 34 - Shell and Merge Sorts 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?