Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3: The Efficiency of Algorithms

Similar presentations


Presentation on theme: "Chapter 3: The Efficiency of Algorithms"— Presentation transcript:

1 Chapter 3: The Efficiency of Algorithms
Invitation to Computer Science, C++ Version, Fourth Edition

2 Objectives In this chapter, you will learn about
Attributes of algorithms Measuring efficiency Analysis of algorithms Invitation to Computer Science, C++ Version, Fourth Edition

3 Introduction Desirable characteristics in an algorithm Correctness
Ease of understanding (clarity) Elegance Efficiency Invitation to Computer Science, C++ Version, Fourth Edition

4 Attributes of Algorithms
Correctness Does the algorithm solve the problem it is designed for? Does the algorithm solve the problem correctly? Ease of understanding (clarity) How easy is it to understand or alter the algorithm? Important for program maintenance Invitation to Computer Science, C++ Version, Fourth Edition

5 Attributes of Algorithms (continued)
Elegance How clever or sophisticated is the algorithm? Sometimes elegance and ease of understanding work at cross-purposes Efficiency How much time and/or space does the algorithm require when executed? Perhaps the most important desirable attribute Invitation to Computer Science, C++ Version, Fourth Edition

6 Quiz 1.(T/F)The correctness of algorithms is important for program maintenance. 2.(T/F)The clarity of alogrithms is the most important desirable attribute. 3.(T/F)The elegance of algorithms describes the time and space of algorithms execution. 4.(T/F)The efficiency of algorithms describes the results of algorithms execution. Invitation to Computer Science, C++ Version, Fourth Edition

7 Measuring Efficiency Analysis of algorithms
Study of the efficiency of various algorithms For one input size, best case, worst case, and average case behavior must be considered The  notation captures the order of magnitude of the efficiency function (n): order of magnitude n. Order of magnitude is used to measure the time efficiency of algorithms. Invitation to Computer Science, C++ Version, Fourth Edition

8 What is Order of Magnitude?
Kim makes $1 dollar an hour and asks her coworker John about his pay. John told her that his pay is 2 order of magnitude higher than her pay. What exactly does John make? A. $1 B. $2 C. $10 D. $100 E. $20 Invitation to Computer Science, C++ Version, Fourth Edition

9 What is Order of Magnitude?
Kim makes $10 dollar an hour and asks her coworker John about his pay. John told her that his pay is 2 order of magnitude higher than her pay. What exactly does John make? A. $1 B. $2 C. $10 D. $100 E. $20 Invitation to Computer Science, C++ Version, Fourth Edition

10 What is () or Big 0? Orders of magnitude are generally used to make very approximate comparisons. If two numbers differ by one order of magnitude, one is about ten times larger than the other. If they differ by two orders of magnitude, they differ by a factor of about 100. 1 - (1) 10 - (2) 100 - (3) Invitation to Computer Science, C++ Version, Fourth Edition

11 How quickly the values grow!
Orders of magnitude (n) determines how quickly the values grow as n increases. You want to write an algorithm with an order of magnitude that grow slowly as n increases. For example, which one of the following algorithm has the shortest execution time? John’s algorithm: (1) Kim’s algorithm: (n) Mathew’s algorithm: (n2) Mark’s algorithm: (lg n) (note: logarithm of n to the base 2) Answers: When the data set size n is 4, the total execution time of John’s algorithm is 1 Kim’s algorithm is 4 Mathew’s algorithm is 16 Mark’s algorithm is 2 When the data set size n is 16, the total execution time of Kim’s algorithm is 16 Mathew’s algorithm is 256 Mark’s algorithm is 4 Invitation to Computer Science, C++ Version, Fourth Edition

12 Sequential Search Invitation to Computer Science, C++ Version, Fourth Edition

13 Sequential Search (continued)
Comparison of the NAME being searched for against a name in the list Central unit of work Used for efficiency analysis For lists with n entries Best case NAME is the first name in the list 1 comparison (1) Invitation to Computer Science, C++ Version, Fourth Edition

14 Sequential Search (continued)
For lists with n entries Worst case NAME is the last name in the list NAME is not in the list n comparisons (n) Average case Roughly n/2 comparisons Invitation to Computer Science, C++ Version, Fourth Edition

15 Sequential Search (continued)
Space efficiency Uses essentially no more memory storage than original input requires Very space efficient Invitation to Computer Science, C++ Version, Fourth Edition

16 Selection Sort Sorting Selection sort algorithm
Take a sequence of n values and rearrange them into order Selection sort algorithm Repeatedly searches for the largest value in a section of the data Moves that value into its correct position in a sorted section of the list Invitation to Computer Science, C++ Version, Fourth Edition

17 Shows the data list after each selection sort swap (exchange) on the following data set:
1st swap: 2nd swap: 3rd swap: 4th swap: Invitation to Computer Science, C++ Version, Fourth Edition

18 Quiz Use the Sort Animator to answer the question: How many times has Step 8 been executed? _____
Invitation to Computer Science, C++ Version, Fourth Edition

19 Work = cn for Various Values of c
Figure 3.4 Work = cn for Various Values of c Invitation to Computer Science, C++ Version, Fourth Edition

20 Selection Sort (continued)
Count comparisons of largest so far against other values Find Largest, given m values, does m-1 comparisons Selection sort calls Find Largest n times, Each time with a smaller list of values Cost = n-1 + (n-2) + … = n(n-1)/2 Invitation to Computer Science, C++ Version, Fourth Edition

21 Selection Sort (continued)
Time efficiency Comparisons: n(n-1)/2 Exchanges: n (swapping largest into place) Overall: (n2), best, average and worst cases Space efficiency Space for the input sequence, plus a constant number of local variables Invitation to Computer Science, C++ Version, Fourth Edition

22 Quiz (T/F)The best case of the time efficiency of sequential search algorithm is (n2). (T/F)The average case of the time efficiency of sequential search algorithm is (n). (T/F)The worst case of the time efficiency of sequential search algorithm is (n2). Invitation to Computer Science, C++ Version, Fourth Edition

23 Quiz (T/F)The best case of the time efficiency of selection sort algorithm is (n2). (T/F)The aerage case of the time efficiency of selection sort algorithm is (n). (T/F)The worst case of the time efficiency of selection sort algorithm is (n2). Invitation to Computer Science, C++ Version, Fourth Edition

24 Quiz 1.(T/F) Order of magnitude n is written (n).
2.(T/F) Order of magnitude is used to measure the space efficiency of algorithms. Invitation to Computer Science, C++ Version, Fourth Edition

25 Quiz __(T/F) If each comparison takes about 1 second, then the total time for a sequential search algorithm to search an item in a list with 100 entries at the best case is (1) seconds. __(T/F) If each comparison takes about 1 second, then the total time for a sequential search algorithm to search an item in a list with 100 entries at the worst case is (1) seconds. Invitation to Computer Science, C++ Version, Fourth Edition

26 __(T/F) If each comparison takes about 1 second, then the total time for a sequential search algorithm to search an item in a list with 100 entries at the average case is (1) seconds. Invitation to Computer Science, C++ Version, Fourth Edition

27 __(T/F) If each comparison takes about 1 second, then the total time for a selection sort algorithm to search an item in a list with 10 entries at the best case is (10) seconds. __(T/F) If each comparison takes about 1 second, then the total time for a selection sort algorithm to search an item in a list with 100 entries at the average case is (1) seconds. Invitation to Computer Science, C++ Version, Fourth Edition

28 __(T/F) If each comparison takes about 1 second, then the total time for a selection sort algorithm to search an item in a list with 100 entries at the worst case is (1) seconds. Invitation to Computer Science, C++ Version, Fourth Edition

29 Binary Search Algorithm
Given ordered data Search for NAME by comparing to middle element If not a match, restrict search to either lower or upper half only Each pass eliminates half the data Invitation to Computer Science, C++ Version, Fourth Edition

30 Binary Search Algorithm (list must be sorted)
Figure 3.18 Binary Search Algorithm (list must be sorted) Invitation to Computer Science, C++ Version, Fourth Edition

31 Binary Search Algorithm (continued)
Efficiency Best case 1 comparison (1) Worst case lg n comparisons lg n: The number of times n can be divided by two before reaching 1 (lg n) Invitation to Computer Science, C++ Version, Fourth Edition

32 Binary Search Exercise
Use the binary search algorithm to decide whether 35 is in the following list: 3, 6, 7, 10, 35, 37, 39 What numbers will be compared to 35? Ans: 10 37 35 Invitation to Computer Science, C++ Version, Fourth Edition

33 Binary Search Quiz Use the binary search algorithm to decide whether 2 is in the following list: 3, 6, 7, 10, 35, 37, 39 What numbers will be compared to 2? Ans: Invitation to Computer Science, C++ Version, Fourth Edition

34 Answers of #28 on page 123. #28a. 100 10 ------ = 500 50 #28b
= #28b 100* = 500 * Invitation to Computer Science, C++ Version, Fourth Edition


Download ppt "Chapter 3: The Efficiency of Algorithms"

Similar presentations


Ads by Google