Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright  1997 Oxford University Press All Rights Reserved

Similar presentations


Presentation on theme: "Copyright  1997 Oxford University Press All Rights Reserved"— Presentation transcript:

1 Copyright  1997 Oxford University Press All Rights Reserved
Transparency Masters for Chapter 5 of Data Structures via C++ Objects by Evolution A. Michael Berman Copyright  1997 Oxford University Press All Rights Reserved

2 Copyright  1997 Oxford University Press All Rights Reserved
Chapter 5 Efficiency Overview An introduction to analyzing the efficiency of algorithms, illustrated through sorting and searching. 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

3 Copyright  1997 Oxford University Press All Rights Reserved
Chapter Objectives 1. To begin to understand methods for analyzing the efficiency of algorithms. 2. To understand and analyze the Linear Search and Binary Search algorithms. 3. To understand and analyze the Selection Sort and Bubble Sort algorithms. 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

4 Copyright  1997 Oxford University Press All Rights Reserved
Exercise 5-1 Based on the discussion in the book and/or in class, and upon your own insights, list the considerations that can be used to evaluate the desirability of an algorithm. 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

5 Copyright  1997 Oxford University Press All Rights Reserved
Definition 5-1 The asymptotic efficiency of an algorithm describes the relative efficiency of an algorithm as n gets very large. 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

6 Copyright  1997 Oxford University Press All Rights Reserved
Exercise 5-2 Rank the asymptotic orders of the following functions, from highest to lowest. (You may wish to graph some or all of them.) 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

7 Figure 5-1: Sample record format
key data 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

8 Algorithm 5-1: Linear Search algorithm
For each item in the list if the item’s key matches the target, stop and report “success” Report “failure” 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

9 Code Example 5-1: Linear Search
// cx5-1.cpp // Code Example 5-1: Linear Search int linearSearch(int a[], int n, int target) { int i; //Critical Operation begin for (i = 0; i < n; i++) if (a[i] == target) // key comparison return i; //Critical Operations end return -1; // use -1 to indicate failure } 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

10 Copyright  1997 Oxford University Press All Rights Reserved
Linear Search: 3 Cases Best Case f(n) = 1 Worst Case f(n) = n Average Case Assumptions? 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

11 Linear Search: Average Case
The probability that target is in the array is 50%: the first term If the target is in the array, then its position is uniformly distributed: the second term 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

12 Figure 5-2: A graph of the average case performance of Linear Search
5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

13 Copyright  1997 Oxford University Press All Rights Reserved
Exercise 5-3 For the following formulas, identify the high- order term and indicate the order of each using big-O notation. 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

14 Copyright  1997 Oxford University Press All Rights Reserved
Exercises 5-4 The standard Linear Search algorithm assumes that the items being searched are static, that is, each item remains in the same position for every search. Suppose you are allowed to move the data around while you’re searching  can you suggest a method for making the search more efficient? 5-5 Write a driver for the function linearSearch (Code Example 5-1) that creates a large array and fills it with random data. 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

15 Copyright  1997 Oxford University Press All Rights Reserved
Exercise 5-6 Analyze linear search under the following assumptions: 1. The target item is found 90% of the time. 2. The target item is found every time. For these assumptions, is linear search still O(n)? Are there any assumptions about the probability of finding the target item that would make linear search anything but linear? Explain. 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

16 Copyright  1997 Oxford University Press All Rights Reserved
Exercises 5-7 Design and implement a driver similar to the one in Exercise 5-5, but make sure that the search data never contains a duplicated element. 5-8 Using the C++ standard String class, modify your driver and the linearSearch function to perform a search of strings instead of ints. (Because the strings take more room than ints you may need to reduce the size of your test array. 5-9 Make a list of, say, 20 items, and practice Binary Search. Try items in the list and items not in the list. 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

17 Copyright  1997 Oxford University Press All Rights Reserved
Equation 5-2 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

18 Copyright  1997 Oxford University Press All Rights Reserved
Binary Search Assumption: Records are stored in (ascending) sorted order. Algorithm Pick up the middle element a[mid]. If they match, we are done. If not, the compare a[mid] and key. If key < a[mid], go left. If key > a[mid], to right. 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

19 Table 5-1: The growth of the base-2 log function
5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

20 Graph illustrating relative growth of linear and logarithmic functions
5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

21 Code Example 5-2: Defective Binary Search
// cx5-2.cpp // Code Example 5-2: Defective Binary Search -- Do Not Use! int binarySearch(int a[], int n, int target) { // Precondition: array a is sorted in ascending order from a[0] to a[n-1] int first(0); int last(n - 1); int mid; while (first <= last) { mid = (first + last)/2; if (target == a[mid]) return mid; 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

22 Code Example 5-2: Defective Binary Search
else if (target < a[mid]) last = mid; else // must be that target > a[mid] first = mid; } return -1; // use -1 to indicate item not found 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

23 Figure 5-4 Illustrated Invariant for Binary Search
5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

24 Table 5-2: Trace of Code Example 5-2 on Page 99
5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

25 Code Example 5-3: Verified binary Search
int binarySearch(int a[], int n, int target) { // Precondition: array a is sorted in ascending order from a[0] to a[n-1] int first(0); int last(n - 1); int mid; while (first <= last) { // Invariant: if target in a, then a[first] <= target <= a[last] mid = (first + last)/2; 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

26 Code Example 5-3: Verified binary Search
if (target == a[mid]) return mid; else if (target < a[mid]) last = mid - 1; else // must be that target > a[mid] first = mid + 1; } return -1; //use -1 to indicate item not found 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

27 Copyright  1997 Oxford University Press All Rights Reserved
Exercise 5-10 5-10 Write a driver for binarySearch, and test it with the defective binary sssearch in Code Example Give an example of an input where it returns an anser, and an input where it goes into an endless loop. 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

28 Copyright  1997 Oxford University Press All Rights Reserved
Algorithm 5-2 Max position <-- pposition of first item in the array current position <-- position of second item in the array while current position is not past the end of the array do if item in current position is greater than item in max position then max position <-- current position current position <-- position of next item in the list return max position Exercise 5-11 5-11 Write and analyz a min selection algorythm. 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

29 Code Example 5-4: The maxSelect function
// cx5-4.cpp // Code Example 5-4: Max Select function int maxSelect(int a[], int n) { int maxPos(0), currentPos(1); while (currentPos < n) { // Invariant: a[maxPos] >= a[0] ... a[currentPos-1] if (a[currentPos] > a[maxPos]) maxPos = currentPos; currentPos++; } return maxPos; 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

30 Figure 5-5: The operation of Selection Sort
5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

31 Code Example 5-5: Selection Sort
// cx5-5.cpp // Code Example 5-5: Selection Sort void swapElements(int a[], int maxPos, int last); // see Exercise 5-12 int maxSelect(int a[], int n); // see cx5-4.cpp void selectionSort(int a[], int n) { int last(n-1); int maxPos; 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

32 Code Example 5-5: Selection Sort
while (last > 0) { // invariant: a[last+1] ... a[n-1] is sorted && // everything in a[0] ... a[last] <= everything in a[last+1] ... a[n-1] maxPos = maxSelect(a, last+1); // last+1 is length from 0 to last swapElements(a, maxPos, last); last--; } 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

33 Copyright  1997 Oxford University Press All Rights Reserved
Exercise 5-12 Write swapElements. Equation 5-3 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

34 Copyright  1997 Oxford University Press All Rights Reserved
Equation 5-4 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

35 Copyright  1997 Oxford University Press All Rights Reserved
Equation 5-5 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

36 Copyright  1997 Oxford University Press All Rights Reserved
Equation 5-6 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

37 Example of one phase of Bubble Sort
5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

38 Code Example 5-6: bubbleSortPhase
// cx5-6.cpp // Code Example 5-6: Bubble Sort Phase void swapElements(int a[], int maxPos, int last); // see Exercise 5-12 void bubbleSortPhase(int a[], int last) { // Precondition: a is an array indexed from a[0] to a[last] // Move the largest element between a[0] and a[last] into a[last], // by swapping out of order pairs int pos; 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

39 Code Example 5-6: bubbleSortPhase
for (pos = 0; pos < last - 1; pos++) if (a[pos] > a[pos+1]) { swapElements(a, pos, pos+1); } // Postconditions: a[0] ... a[last] contain the same elements, // possibly reordered; a[last] >= a[0] ... a[last-1] 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

40 Code Example 5-7: Bubble Sort
// cx5-7.cpp // Code Example 5-7: Bubble Sort void bubbleSortPhase(int a[], int last); // see cx5-6.cpp void bubbleSort(int a[], int n) { // Precondition: a is an array indexed from a[0] to a[n-1] int i; for (i = n - 1; i > 0; i--) bubbleSortPhase(a, i); // Postcondition: a is sorted } 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

41 Copyright  1997 Oxford University Press All Rights Reserved
Equation 5-7 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

42 Copyright  1997 Oxford University Press All Rights Reserved
Chapter Summary Abstract Data Types provide a powerful tool for organizing software. Code reuse makes programmers more efficient. Information hiding and encapsulation lead to simpler and safer software. You use a class to create an encapsulated ADT in C++. Standard C++ libraries provide an important source of reusable code. Using inheritance and polymorphism with encapsulated ADT’s is called Object-Oriented Programming. 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

43 Programming Laboratory Problems
5-1 Using a driver and the corrected binary searh (Code Example 5-3 on page 101), test the number of comparisons performed by binary search. To do so, you’ll need to modify the binarySearch function to keep track of the number of comparisons performed and returned by this number each time it’s called. Make sure you catch every comarison! Perform 50 tests on different random targets (in each case, use an array in which a[I] = I) for th following values of n: (if possible on your system) 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

44 Programming Laboratory Problems
5-2 Write a driver using the Timer class9 that recordes the elapsed time required by calls to function. Record the time required by 50 searches for 101 through 107 (if possible). If you have a very fast processor and a very crude clock (such as provided by DOS) you may need to increase the number of searches to get meaningful results. Graph the times and compare them to the results from Lab 5-1, and to the hypothesized asymptotic analysis curve as shown in Figure 5-3 on Page 99. 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

45 Programming Laboratory Problems
5-3 Modify binarySearch to search for strings, using the C++ strings library. Record the times required by the searches. (You may have to settle for fewer serches because of the extra time and space required by the strings.) Graph the results and compare with the results from previous labs, if don, and to the hypothesized asymptic analysis curve as shown in Fughre 5-3 on Page 99. 5-4 Using the Timer Class, compare the running times of the original Bubble Sort to Buubble Sort as modified in Exercise Use arrays of size from 101 up to 105 and at least 50 randomly-generated arrays for each size.10 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

46 Programming Laboratory Problems
5-5 Using the Timer Class, compare the running times for (modified) Bubble Sort, Selection Sort, and Insertin Sort. Which algorithm appears to be the fastest? How do the shapes of the performance graphs for the three sorts differ? 5-6 Interpolation Search is a variant of Binary Search that can be faster on data that’s “evenly distributed”11 in standard Binary Search, the middle element is chosen using the formular: (continued on next slide) 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

47 Programming Laboratory Problems
Perl et al proved in 1798 that Interpolation Search requires O(loglogn) comparisons, somewhat faster than binary Search. Modify the Binary Search program to create an Interpolation Search function, and compare it to Binary Search. Generate your search arrays using the following method. For an array of size n: 1. Pick each element in the range from 0 to 10n. 2. Sort the array. 3. Pick the target by choosing a random value I between 0 and n - 1, and using the value in a[I]. 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

48 Programming Laboratory Problems
Using a version of Interpolation Search that keeps track of the number of comparisons performaed, run the search on ten randomly chosen arrays, using ten random targets (for a total of runs per value of n), for the following values of n: 10 1, ,000 ,000 Run Binary Search for the same randomly chosen data. Plot the results. Did Interpolation Search do better than Binary Search for your data? (They will probably be quite close in performance.) 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

49 Programming Laboratory Problems
5-7 Linear Search with Move to Front (LSMF) works as follows: each time you search, move the key that’s found one step closer to the front of the array. For example, suppose your array contains: and you search for 14. After you find it, you move it one step toward the front by exchanging it with 9; so the array will now look like: The potentially good thing about LSMF is that if some items are much more commonly retrieved, they end up clustering near the front of the array, where they can be found quickly, while rarely retrieved items move toward the back. (continued on next slide) 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

50 Programming Laboratory Problems
1. Analyze the time complexity of LSMF: how many comparisons does it require in the worst case? 2. Implement LSMF, along with a test driver that creates random lists for searching. You’ll also need a sort routine to prepare the list used by Binary Search. Use a separate array for each search, of size 210= elements. Fill the list with random numbers between 0 and Add a counter to the LSMF function that will count the number of comparisons performed. Thoroughly test your algorithm by selecting random search keys calling the LSMF function. (continued on next slide) 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

51 Programming Laboratory Problems
3. Now compare LSMF to standard Linear Search. LSMF only work well if certain items are accessed more frequently than others (why?) To evluate LSMF, we’ll use the following procedure: n = 1024 Fill the array with random numbers (in the range 0 to 1,000,000) Find 10 numbers that are in the array -you can use the numbers at 100, 200,300,…. Call these the “hot numbers”-the ones that will be accessed frequently. (continued on next slide) 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

52 Programming Laboratory Problems
In some searches, you will look for numbers randomly, while in others you will only look for the “hot numbers”. First trial: 50% of the time, select (randomly) a “hot number”; 50% of the time, choose any number at random. Second trial: 90% of the time, select a hot number; 10% of the time, choose any number at random. (continued on next slide) 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved

53 Programming Laboratory Problems
Run each trial 100 times. Be sure to completely reset everything and pick new random numbers each time. Describe your results. How does LSMF compare to linear search? How does the first trial compare to the second trial? When is LSMF a good algorithm to use? 5/6/2019 Copyright  1997 Oxford University Press All Rights Reserved


Download ppt "Copyright  1997 Oxford University Press All Rights Reserved"

Similar presentations


Ads by Google