Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 101 Efficiency and Complexity Analysis.

Similar presentations


Presentation on theme: "Computer Science 101 Efficiency and Complexity Analysis."— Presentation transcript:

1 Computer Science 101 Efficiency and Complexity Analysis

2 Things to Desire in an Algorithm Correctness Maintainability Efficiency

3 Measuring Efficiency Empirical - use clock to get actual running times for different inputs Problems: –Different machines have different running times –Some running times are so long that it is impractical to check them

4 Measuring Efficiency Analytical - use pencil and paper to determine the abstract amount of work that a program does for different inputs Advantages: –Machine independent –Can predict running times of programs that are impractical to run

5 Complexity Analysis Pick an instruction that will run most often in the code Determine the number of times this instruction will be executed as a function of the size of the input data Focus on abstract units of work, not actual running time

6 Example: Search for Largest We focus on the comparison ( > ) inside the loop and ignore the other instructions. for a list of length 1, zero comparisons for a list of length 2, one comparison. for list of length N, N - 1 comparisons set Largest 1 set Current to 2 while Current <= N do if A(Current) > A(Largest) then set Largest to Current increment Current

7 Big-O Notation Big-O notation expresses the amount of work a program does as a function of the size of the input O(n) stands for order of magnitude n, or order of n for short Search for the largest is O(n), where n is the size of the input

8 Common Orders of Magnitude ConstantO(k) LogarithmicO(log 2 n) LinearO(n) QuadraticO(n 2 ) ExponentialO(k n )

9 Common Orders of Magnitude n O(log 2 n) O(n) O(n 2 )O(2 n ) digits!

10

11 Recall that search for a value required exactly 3N + 3 steps in the worst case As N gets very large, the difference between N and N + K becomes negligible (where K is a constant) As N gets very large, the difference between N and N / K or N * K also becomes negligible Use the highest degree term in a polynomial and drop the others (N 2 – 2N + 2)  N 2 Approximations

12 Example Approximations n O(n) O(n) + 2 O(n 2 ) O(n 2 ) + n

13 Analysis of Selection Sort The main loop runs approximately N times Thus, the search for largest algorithm runs N times The loop within the ith run of search for largest runs N - i times

14 Analysis of Selection Sort Overall, the number of comparisons performed by the search for the largest value within selection sort is N N N = (N 2 – N)/2  N 2 Selection sort is a quadratic algorithm

15 Other Algorithms (Worst Cases) Search Search for largestO(?) Sequential search for a target valueO(?) Binary search for a target valueO(?) Sorting Bubble sortO(?) Quick sortO(?)


Download ppt "Computer Science 101 Efficiency and Complexity Analysis."

Similar presentations


Ads by Google