1 / 8

Mergesort

Department of Computer and Information Science, School of Science, IUPUI. Mergesort. Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu. Computational Complexity. Framework to study efficiency of algorithms. Example = sorting.

nakia
Download Presentation

Mergesort

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. Department of Computer and Information Science,School of Science, IUPUI Mergesort Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

  2. Computational Complexity • Framework to study efficiency of algorithms. Example = sorting. • MACHINE MODEL = count fundamental operations. • count number of comparisons • UPPER BOUND = algorithm to solve the problem (worst-case). • N log2 N from mergesort • LOWER BOUND = proof that no algorithm can do better. • N log2 N - N log2 e • OPTIMAL ALGORITHM: lower bound ~ upper bound. • mergesort

  3. a1 < a2 YES NO a2 < a3 a1 < a3 YES NO YES NO a1 < a3 a2 < a3 YES NO YES NO Decision Tree printa1, a2, a3 printa2, a1, a3 printa1, a3, a2 printa3, a1, a2 printa2, a3, a1 printa3, a2, a1

  4. Comparison Based Sorting Lower Bound • Theorem. Any comparison based sorting algorithm must use (N log2N) comparisons. • Proof. Worst case dictated by tree height h. • N! different orderings. • One (or more) leaves corresponding to each ordering. • Binary tree with N! leaves must have height • Food for thought. What if we don't use comparisons? • Stay tuned for radix sort. Stirling's formula

  5. Mergesort Analysis • How long does mergesort take? • Bottleneck = merging (and copying). • merging two files of size N/2 requires N comparisons • T(N) = comparisons to mergesort N elements. • to make analysis cleaner, assume N is a power of 2 • Claim. T(N) = N log2 N. • Note: same number of comparisons for ANY file. • even already sorted • We'll prove several different ways to illustrate standard techniques.

  6. void merge(Item a[], int left, int mid, int right) <999>{ int i, j, k; for (<999>i = mid+1; <6043>i > left; <5044>i--) <5044>aux[i-1] = a[i-1]; for (<999>j = mid; <5931>j < right; <4932>j++) <4932>aux[right+mid-j] = a[j+1]; for (<999>k = left; <10975>k <= right; <9976>k++) if (<9976>ITEMless(aux[i], aux[j])) <4543>a[k] = aux[i++]; else <5433>a[k] = aux[j--]; <999>} void mergesort(Item a[], int left, int right) <1999>{ int mid = <1999>(right + left) / 2; if (<1999>right <= left) return<1000>; <999>mergesort(a, aux, left, mid); <999>mergesort(a, aux, mid+1, right); <999>merge(a, aux, left, mid, right); <1999>} Mergesort prof.out Profiling Mergesort Empirically Striking feature:All numbers SMALL! # comparisonsTheory ~ N log2 N = 9,966Actual = 9,976

  7. Sorting Analysis Summary • Running time estimates: • Home pc executes 108 comparisons/second. • Supercomputer executes 1012 comparisons/second. • Lesson 1: good algorithms are better than supercomputers. • Lesson 2: great algorithms are better than good ones. Insertion Sort (N2) Mergesort (N log N) computer thousand million billion thousand million billion home instant 2.8 hours 317 years instant 1 sec 18 min super instant 1 second 1.6 weeks instant instant instant Quicksort (N log N) thousand million billion instant 0.3 sec 6 min instant instant instant

  8. Acknowledgements • Sorting methods are discussed in our Sedgewick text. Slides and demos are from our text’s website at princeton.edu. • Special thanks to Kevin Wayne in helping to prepare this material.

More Related