90 likes | 222 Views
Class 7: Complexity & Sorting. Why Big O complexity?. There are many functions (every program is a function) Sizes of inputs vary enormously Rates of growth differ enormously (see table) We can’t see the forest for the trees Solution: groups functions into complexity classes.
E N D
Why Big O complexity? • There are many functions (every program is a function) • Sizes of inputs vary enormously • Rates of growth differ enormously (see table) • We can’t see the forest for the trees • Solution: groups functions into complexity classes cis 335 Fall 2001 Barry Cohen
Big O rules • Ignore all but the fastest growing term • Ignore a multiplicative constant in the remaining term • Add together growth rate functions cis 335 Fall 2001 Barry Cohen
Some practice • Is O(log2n) = O(log10n)? • Which grows faster,1,000,000n2 or n3? • Which grows faster,n10 or 2n/10? cis 335 Fall 2001 Barry Cohen
Worst case, average case,best case • Big O is worst case. It is a performance guarantee. • It’s usually the most important measure. (And the one we can most easily get.) • Sometimes average case is most common (Qsort) • Crypto: a different yardstick cis 335 Fall 2001 Barry Cohen
Selection sort • How the algorithm works (like sorting a hand of cards) • What is its Big O complexity? • What is its average case complexity? • At 1,000 comparisons/sec, how long would it take to sort 100,000 items? cis 335 Fall 2001 Barry Cohen
Merge sort • How the algorithm works • Big O complexity. Is the work being done in the divide or the combine phase? • Does the order of the input matter? If so, what are the best and worst cases? • What is the space requirement of mergesort? What is its Big O space complexity? cis 335 Fall 2001 Barry Cohen
Quicksort • Another divide and conquer • Basic idea: * partition around a pivot* recurse on each of the parts • Where is most of the work done? • What is the best case, and how long does it take? • What is the worst case, and how long does it take? cis 335 Fall 2001 Barry Cohen
Radix sort • Method: sort into buckets. No comparison of elements! • Analysis we break the n log n barrier. • What is the space requirement? • How long does it take to retrieve the answer? cis 335 Fall 2001 Barry Cohen