Download presentation

Presentation is loading. Please wait.

Published byBrandon Stokes Modified over 2 years ago

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 ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5 32 1024 4294967296 64 6 64 4096 19 digits! 128 7 128 16384 256 8 256 65536 512 9 512 262144 1024 10 1024 1048576

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 2 2 4 4 6 4 4 6 16 20 8 8 10 64 72 16 16 18 256 272 32 32 34 1024 1056 64 64 66 4096 5050 128 128 130 16384 16512 256 256 258 65536 65792 512 512 514 262144 262656 1024 1024 1026 1048576 1049600

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 - 1 + N - 2 + N - 3 +.. + 1 = (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(?)

Similar presentations

Presentation is loading. Please wait....

OK

1 Algorithms Searching and Sorting Algorithm Efficiency.

1 Algorithms Searching and Sorting Algorithm Efficiency.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google