Mergesort - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

Mergesort

Description:

Framework to study efficiency of algorithms. Example = sorting. ... Stay tuned for radix sort. Stirling's formula. Dale Roberts. Mergesort Analysis ... – PowerPoint PPT presentation

Number of Views:120
Avg rating:3.0/5.0
Slides: 9
Provided by: dalero
Category:
Tags: mergesort | radix

less

Transcript and Presenter's Notes

Title: Mergesort


1
Mergesort
Department of Computer and Information
Science,School of Science, IUPUI
Dale Roberts, Lecturer Computer Science,
IUPUI E-mail droberts_at_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
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
Profiling Mergesort Empirically
Striking featureAll 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 texts
    website at princeton.edu.
  • Special thanks to Kevin Wayne in helping to
    prepare this material.
Write a Comment
User Comments (0)
About PowerShow.com