Discrete Math CSC151 Analysis of Algorithms
Complexity of Algorithms In CS it's important to be able to predict how many resources an algorithm will use space (in RAM, on disk) time Too expensive to implement everything then test it Too hard to do precise analysis Want a quick and easy solution
Analysis of a sample program Linear search start at first item, and compare if it's the right one. Move on to next if not Searching for 6 in matches!
Analysis of a sample program Bubble Sort compare each element with its neighbor and swap if they're out of order Pass Pass Pass Pass 4-6 don't change anything.
Analysis of bubble sort Each comparison requires t1 time Each exchange requires t2 time If there are n items, then there are n passes Pass p requires n-p comparisons Total time is somewhere between (n-1 + n )*t1 and (n-1 + n )*(t1+t2) time
Analysis of a sample program Insert Sort take a list of one and add in other elements until they're sorted
Analysis of Insert sort Each comparison requires t1 time Each exchange requires t2 time If there are n items, we must insert n-1 times Inserting element m requires between 1 and m-1 comparison/exchanges