1 / 9

CS200: Algorithm Analysis

CS200: Algorithm Analysis. MORE ON SORTING. How fast can we sort n items if we use a comparison sort?____________ Comparison sorts require comparison of elements to induce order: Mergesort, Quicksort, Heapsort, Insertion sort...etc.

stuartr
Download Presentation

CS200: Algorithm Analysis

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. CS200: Algorithm Analysis

  2. MORE ON SORTING • How fast can we sort n items if we use a comparison sort?____________ • Comparison sorts require comparison of elements to induce order: Mergesort, Quicksort, Heapsort, Insertion sort...etc. • A comparison sort must do at least n comparisons in order to compare n elements: but what about the large gap between n and n log n? Can we do better than n log n?

  3. Why comparison sorts must be at least n log n? • Argument uses a Decision Tree abstraction that represents the comparisons performed by a sorting algorithm on an input of size n. • Show the decision tree for insertion sort on A = <a1,a2,a3> (next slide). Note that there are n! leaves in the tree corresponding to all possible permutations of n input elements. • Each interior node is annotated with the relationship (≤, >) between ai and aj; elements in the set to be sorted. • Each leaf is annotated by a permutation of n.

  4. Tracing Insertion sort requires finding a path from the root to a leaf in the decision tree. • Trace Insertion sort for A= <9,2,6>. • The longest path for a decision tree that models Insertion sort is Q(n2) and for MergeSort is Q(n log n). This represents the worst-case number of comparisons. • A lower bound on the height of a decision tree thus gives a lower bound on the running time of a comparison sort.

  5. Theorem: Any decision tree that sorts n elements has a height of h = W(n log n). Proof: a decision tree has n! leaves. A decision tree must be a binary tree because a split at a sub-root depends on the two relationships; ≤ and >. A binary tree of height h has no more than 2h leaves (by definition, see Appendix B, done in lecture). n! ≤ 2h, lemma proved next, see Appendix B. Take log of both sides, log(n!) ≤ log(2h) h ≥ log(n!) for n! use, n! > (n/e)n By Stirlings approx. h ≥ log(n/e)n log(n/e)n = n log n - n log e n log e is a lower order term. h = W(n log n)

  6. Lemma - Now to prove the lemma: Proof: By induction on h. Basis: h = 0. Tree is just one node, which is a leaf. 2h= 1. Inductive step: Assume true for height = h − 1. Extend tree of height h − 1 by making as many new leaves as possible. Each leaf becomes parent to two new leaves. # of leaves for height h = 2·(# of leaves for height h−1) = 2 · 2h-1 (ind. hypothesis) • • • • = 2h.

  7. By Theorem the following holds: mergesort, heapsort and median partitioned quicksort are optimal sorting algorithms.

  8. Summary • Lower bound on sorts • Theorem 8.1 and corollary 8.2

More Related