ALGORITHM ANALYSIS INTRO Just the basics here Algorithm

ALGORITHM ANALYSIS INTRO Just the basics here

Algorithm Analysis for Dummies* Algorithm Analysis definition: • comparing algorithms based upon the amount of computing resources that each algorithm uses • Worry about time the algorithm takes to run • Worry about the space an algorithm takes up *There’s a whole course on this – clearly this is just a very simple introduction!!! *And I’m not implying you’re dumb!!!!!

Algorithm Analysis for Dummies Why? • Gives us a systematic, theoretical estimate of the efficiency of an algorithm • • Could just run algorithm and measure time: why don’t we do this? • Everyone’s computer is different – different ram, different processor, etc. • Also, we don’t know what a person has running simultaneously on their computer Instead, Alg. Analysis gives us a systematic way to compare different algorithms’ efficiency • Lets us systematically compare the efficiency of different algorithms whose end result might be the same • E. g. , sorting • MANY sorting algorithms (like, at least a bzillion!) • All result in data that is, well, sorted • (otherwise it’s a pretty crappy sorting algorithm) • So how do you pick which sorting algorithm to use? • How do you know, if you invent a new sorting algorithm, whether it’s better than existing sorting algorithms?

Algorithm Analysis for Dummies • We largely worry about the worst case when we’re comparing algorithms-Big O, or O( ) • We throw away constants and lower degrees • • If the algorithm does the same number of steps, regardless of the size of the data being analyzed, the time analysis is considered to be constant, • • If the analysis amounts to 3 n 2 + 2 n +4 the worst case would be considered O(n 2) And since we drop constants, the analysis would be O(1) If there’s a loop that loops through all the data (or most of the data), that is considered O(n) Because there are n data points • This includes n – 1 and going through all the data, say, 3 times, because we drop constants! •

Algorithm Analysis for Dummies Continued • If there’s a loop that, for every piece of data, calls a loop that goes through every piece of data, that will be O(n 2) i. e. , for (int i= 0; i<n; i++) { for (int j =i; j<n; j++) { works out to be n + n-1 + n-2+n-3… 1 which is (n 2 + n) /2 Drop 2 – it’s a constant Drop n – it’s a lower degree. Get O(n 2)

Algorithm Analysis for Dummies Continued • If there’s a loop and in some way, every time the loop executes, the data is cut in half (ish), that will be: O(log 2 n) i. e. , for (int i= 0; i<n; i++) { n=n/2; • Finally, if there’s either a loop going through all data, and it is around a loop that every time the loop executes cuts the data in half or there’s a loop going through all the data and it is inside the loop that continuously cuts the data in half(ish), that will be: O(nlog 2 n)

Order (worst time analysis to best): Worst O(n 2) O(nlog 2 n) O(log 2 n) Best! O(1)
- Slides: 7