MergeSort

Is on log n the best we can do yes if we use binary

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

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), in-place, 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

Ask a homework question - tutors are online