Download presentation
Presentation is loading. Please wait.
1
Discrete Math CSC151 Analysis of Algorithms
2
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
3
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 3 4 6 8 9 3 4 6 8 9 3 4 6 8 9 matches!
4
Analysis of a sample program Bubble Sort compare each element with its neighbor and swap if they're out of order Pass 1 4 3 8 1 7 6 3 4 8 1 7 6 3 4 1 8 7 6 3 4 1 7 8 6 3 4 1 7 6 8 Pass 2 3 4 1 7 6 8 3 1 4 7 6 8 3 1 4 6 7 8 Pass 3 3 1 4 6 7 8 1 3 4 6 7 8 Pass 4-6 don't change anything.
5
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-2 +... + 2 + 1)*t1 and (n-1 + n-2 +... + 2 + 1)*(t1+t2) time
6
Analysis of a sample program Insert Sort take a list of one and add in other elements until they're sorted 4 - 3 8 1 7 6 3 4 - 8 1 7 6 3 4 8 - 1 7 6 1 3 4 8 - 7 6 1 3 4 7 8 - 6 1 3 4 6 7 8
7
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.