1 / 8

Sorting Algorithms

Sorting Algorithms. Insertion Sort: Θ (n 2 ) Merge Sort: Θ ( nlog (n)) Heap Sort: Θ ( nlog (n)) We seem to be stuck at Θ ( nlog (n)) Hypothesis: Every sorting algorithm requires Ω ( nlog (n)) time. Lower Bound Definitions. Merge sort: Ω ( nlog (n)) on every input Insertion sort:

maralah
Download Presentation

Sorting Algorithms

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. Sorting Algorithms • Insertion Sort: Θ(n2) • Merge Sort:Θ(nlog(n)) • Heap Sort:Θ(nlog(n)) • We seem to be stuck at Θ(nlog(n)) • Hypothesis: Every sorting algorithm requires Ω(nlog(n)) time.

  2. Lower Bound Definitions • Merge sort: Ω(nlog(n)) on every input • Insertion sort: • 1, 2, 3, 4,…, n-1, n  O(n) time • n, n-1, n-2, …, 2, 1  O(n2) time • Hypothesis: For every sorting algorithm A and every integer n, there is some input of length n on which A requires Ω(nlog(n)) time.

  3. Proving Lower Bounds • What if there is some absurd O(n) algorithm? E.g. • Square every third element • For every prime j, A[j] = 2^A[j] • For every j, look up A[j]’th word in the August 2013 New York Times • Etc.

  4. Comparison sorting • Want algorithms that work on any input • e.g. insertion/merge/heap sort • Think about sorting: uses comparisons. • Comparison based sorting algorithm only relies on comparisons + moves • Running time = Ω(# of comparisons)

  5. New Hypothesis Every comparison-based sorting algorithm requires Ω(nlog(n)) comparisons in the worst case.

  6. Required Comparisons • O(nlog(n)) comparisons suffices • merge sort, heap sort • Trivial: need Ω(n/2) comparisons

  7. New Hypothesis Every comparison-based sorting algorithm requires Ω(nlog(n)) comparisons in the worst case. Observation: Tree of height k has at most 2k leaves.

  8. Average Case Analysis • Ω(nlog(n)) comparisons in worst case • Merge sort: always Θ(nlog(n)) • Insertion sort: sometimes Θ(n), sometimes Θ(n2) • Can we get the best of both? Sometimes Θ(nlog(n)), usually O(n)? • NO: need Ω(nlog(n)) comparisons on average among all possible inputs.

More Related