1 / 16

Applied Combinatorics, 4 th Ed. Alan Tucker

Applied Combinatorics, 4 th Ed. Alan Tucker. Section 3.4 Tree Analysis of Sorting Algorithms. Prepared by: Nathan Rounds and David Miller. “Big O” Notation. − I f we have a function f ( x ) defined f ( x ) = 5 x ³ + x – 4,

Download Presentation

Applied Combinatorics, 4 th Ed. Alan Tucker

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Applied Combinatorics, 4th Ed.Alan Tucker Section 3.4 Tree Analysis of Sorting Algorithms Prepared by: Nathan Rounds and David Miller Tucker, Sec. 3.4

  2. “Big O” Notation − If we have a function f (x) defined f (x) = 5x³ + x – 4, we can approximate its value as a constant multiple of x³ for large values of x, since 5x³ is the dominating term in the function (it grows the fastest as x gets large). − For x = 5, f (x) = 626 and the constant multiple 5(x³) = 625 which is a very close estimate. − Therefore, we say that f (x) in “Big O” notation is O(x³). Tucker, Sec. 3.4

  3. Theorem • In the worst case, the number of binary comparisons required to sort n items is at least O(nlog2n). • This number is a constant multiple of (nlog2n) which approximates the height of a binary testing tree (log2n!) for large values of n. Tucker, Sec. 3.4

  4. Bubble Sort For m from 2 to n do: For i from n (step-1) to m do: If Ai Ai-1, then interchange items Ai and Ai-1. 4 6 3 1 5 2 1 4 2 6 3 5 4 6 3 1 2 5 4 6 1 3 2 5 1 4 6 2 3 5 4 1 6 3 2 5 1 2 4 6 3 5 1 4 6 3 2 5 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 This technique moves the smallest number to the top, then the next smallest number below that until the set of numbers is in numerical order. m = 2 i = 3 m = 3 i = 3 m = 3 i = 4 m = 3 i = 5 m = 3 i = 2 m = 2 i = 2 m = 2 i = 4 m = 2 i = 5 m = 2 i = 6 m = 3 i = 6 { Tucker, Sec. 3.4

  5. Bubble Sort Comparisons • When m=2, I goes from n to 2, which means you have to do (n-1) comparisons • When m=3, I goes from n to 3, which means you have to do (n-2) comparisons • Continuing this trend, the total number of comparisons must be: This shows that the Bubble Sort method will require comparisons, which is more then the theoretical bound of . Tucker, Sec. 3.4

  6. 5 4 0 9 2 6 7 1 3 8 67 1 3 8 5 4 0 9 2 0 2 1 8 9 3 5 4 6 7 4 6 7 5 Merge Sort – Setup • Roughly splits the set in half, until only single elements are left. 5 4 0 9 2 6 7 1 3 8 Tucker, Sec. 3.4

  7. 5 4 6 7 3 8 1 0 9 2 4 5 6 7 3 8 2 9 0 4 5 1 6 7 0 2 4 5 9 1 3 6 7 8 0 1 2 3 4 5 6 7 8 9 Merge Sort - Ordering • The set then gets “merged” into numerical order. Note: The root of the tree is on the bottom Tucker, Sec. 3.4

  8. n = 24 Level 0 ++++++++++++++++ Level 1 ++++++++ ++++++++ Level 2 ++++ ++++ ++++ ++++ Level 3 ++ ++ ++ ++ ++ ++ ++ ++ Level 4 + + + + + + + + + + + + + + + + Complexity of Merging • Assume that every set has n = 2r elements • There are r levels • There are 2k vertices on level k • On level k, each set will have 2r-k elements Tucker, Sec. 3.4

  9. Merge Sort Comparisons • In general, at each vertex on level k two ordered sublists of 2r-k-1 items are merged into an ordered sublist of 2r-kitems • This merging requires 2r-k-1 comparisons • The two ordered sublists on level one merge into the root • The number of comparisons needed at level k is 2k (2r-k – 1 ) = 2r - 2k since there are 2k different vertices • Totaling the number of comparisons needed for a Merge Sort is: • With n = 2r this becomes: • This shows that Merge Sort method requires comparisons, which achieves the theoretical bound of a binary search. Tucker, Sec. 3.4

  10. Puts the first element at the end of the left list 7 6 8 9 4 0 1 2 3 5 Once an element is alone there is no need to continue to divide it 0 1 2 3 4 5 6 7 8 9 2 1 3 4 7 6 1 2 9 3 4 8 3 4 1 2 0 QUIK Sort • Takes the first element and compares it with the other elements to divide the list, putting smaller elements into a set on the left and the larger elements into a set on the right. 5 4 0 7 1 6 8 2 3 9 Tucker, Sec. 3.4

  11. Heap Sort • A binary tree so that the parent is always bigger than its children Put the root at the beginning of a list, then move up the next biggest grandchildren. 6 LIST: 5 3 4 2 0 1 Tucker, Sec. 3.4

  12. Class Exercise • Sort the following set of numbers using a bubble sort and then do it using a merge sort. • Which sort technique is more efficient for this set? Tucker, Sec. 3.4

  13. Bubble Sort Solution 5 3 8 23 -6 0 2 66 5 3 8 -6 23 0 2 66 5 3 -6 8 23 0 2 66 5 -6 3 8 23 0 2 66 -6 5 3 8 23 0 2 66 -6 0 5 3 2 8 23 66 -6 0 5 2 3 8 23 66 -6 0 2 5 3 8 23 66 -6 0 2 3 5 8 23 66 . . . Tucker, Sec. 3.4

  14. Merge Sort Solution 5 3 8 23 -6 0 2 66 5 3 8 23 -6 0 2 66 -6 0 2 66 8 23 5 3 3 23 -6 0 66 5 8 2 Tucker, Sec. 3.4

  15. Merge Sort Solution cont’d 3 23 -6 0 66 5 8 2 3 5 8 23 -6 0 2 66 3 5 8 23 -6 0 2 66 -6 0 2 3 5 8 23 66 Tucker, Sec. 3.4

  16. Something to Ponder • How does one create an initial heap to perform a heap sort on? • This will be on the homework.  Tucker, Sec. 3.4

More Related