Unformatted text preview: tion ! Is O(n log n) the best we can do?
– Yes, if we use binary comparison on the key values 19 Theoretic
Theoretic Lower Bound
n elements can have n! permutations
! A sort decision tree is a binary tree that
represents a sorting method based on
comparisons
! Each internal node compares two elements
! – Left branch implies first element smaller than
second
– Right branch otherwise
! Each leaf nodes corresponds to one of the n!
permutations
20 Sort
Sort Decision Tree: An example
Sort 3 items: Never
Never makes a
redundant
comparison
comparison x1,x2 x2,x3
x1<x2<x3 x1,x3
x1,x3 x2<x1<x3 x1<x3<x2 x3<x1<x2 x2,x3 x2<x3<x1 x3<x2<x1 # leaf nodes = n!
leaf 21 Theoretic
Theoretic Lower Bound
A sort decision tree has (2n! – 1) nodes
! Height of tree is at least log n!
! n! ≥ (n/2)(n/2) ⇒ log n! is Ω(n log n)
! Sorting algorithms that use
comparisons must make Ω(n log n)
comparisons
! 22 Quick,
Quick, Merge, or Heap?
! Quicksort is the fastest sorting algorithm in practice,
but
– Can perform as badly as O(n2)
– Requires O(log n) stack space ! Mergesort is guaranteed to run in O(n log n), but
– Slower than quicksort on average
– Requires O(n) additional temporary storage ! Heapsort is guaranteed to run in O(n log n), inplace,
and requires no recursions, but
– Many real world tests show that heapsort is slower than
quicksort (and mergesort) on average
– Useful for really large problems
23...
View
Full
Document
This document was uploaded on 03/06/2014 for the course ECE 368 at Purdue.
 Spring '08
 Staff
 Data Structures, Sort

Click to edit the document details