1 / 37

Data Structures Using C++ 2E

Data Structures Using C++ 2E. Chapter 9 Searching and Hashing Algorithms. Objectives. Learn the various search algorithms Explore how to implement the sequential and binary search algorithms Discover how the sequential and binary search algorithms perform

Télécharger la présentation

Data Structures Using C++ 2E

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms

  2. Objectives • Learn the various search algorithms • Explore how to implement the sequential and binary search algorithms • Discover how the sequential and binary search algorithms perform • Become aware of the lower bound on comparison-based search algorithms • Learn about hashing Data Structures Using C++ 2E 2

  3. Search Algorithms • Item key • Unique member of the item • Used in searching, sorting, insertion, deletion • Number of key comparisons • Comparing the key of the search item with the key of an item in the list • Can use class arrayListType (Chapter 3) • Implements a list and basic operations in an array Data Structures Using C++ 2E 3

  4. Sequential Search • Array-based lists • Covered in Chapter 3 • Linked lists • Covered in Chapter 5 • Works the same for array-based lists and linked lists • See code on page 499 Data Structures Using C++ 2E 4

  5. Data Structures Using C++ 2E seqSearch in array

  6. Sequential Search Analysis • Examine effect of for loop in code on page 499 • Different programmers might implement same algorithm differently • Computer speed affects performance Data Structures Using C++ 2E 6

  7. Sequential Search Analysis (cont’d.) • Sequential search algorithm performance • Examine worst case and average case • Count number of key comparisons • Unsuccessful search • Search item not in list • Make n comparisons • Conducting algorithm performance analysis • Best case: make one key comparison • Worst case: algorithm makes n comparisons Data Structures Using C++ 2E 7

  8. Sequential Search Analysis (cont’d.) • Determining the average number of comparisons • Consider all possible cases • Find number of comparisons for each case • Add number of comparisons, divide by number of cases Data Structures Using C++ 2E 8

  9. Sequential Search Analysis (cont’d.) • Determining the average number of comparisons (cont’d.) Data Structures Using C++ 2E 9

  10. Ordered Lists • Elements ordered according to some criteria • Usually ascending order • Operations • Same as those on an unordered list • Determining if list is empty or full, determining list length, printing the list, clearing the list • Defining ordered list as an abstract data type (ADT) • Use inheritance to derive the class to implement the ordered lists from class arrayListType • Define two classes Data Structures Using C++ 2E 10

  11. Ordered Lists (cont’d.) Data Structures Using C++ 2E 11

  12. FIGURE 9-1 List of length 12 FIGURE 9-2 Search list, list[0]...list[11] FIGURE 9-3 Search list, list[6]...list[11] Binary Search • Performed only on ordered lists • Uses divide-and-conquer technique Data Structures Using C++ 2E 12

  13. Data Structures Using C++ 2E Binary Search Example: sorted array of integer keys. Target=7. [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 0 ] [ 1 ] 3 6 7 11 32 33 53

  14. Data Structures Using C++ 2E Binary Search Example: sorted array of integer keys. Target=7. [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 0 ] [ 1 ] 3 6 7 11 32 33 53 Find approximate midpoint

  15. Data Structures Using C++ 2E Binary Search Example: sorted array of integer keys. Target=7. [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 0 ] [ 1 ] 3 6 7 11 32 33 53 Is 7 = midpoint key? NO.

  16. Data Structures Using C++ 2E Binary Search Example: sorted array of integer keys. Target=7. [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 0 ] [ 1 ] 3 6 7 11 32 33 53 Is 7 < midpoint key? YES.

  17. Data Structures Using C++ 2E Binary Search Example: sorted array of integer keys. Target=7. [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 0 ] [ 1 ] 3 6 7 11 32 33 53 Search for the target in the area before midpoint.

  18. Data Structures Using C++ 2E Binary Search Example: sorted array of integer keys. Target=7. [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 0 ] [ 1 ] 3 6 7 11 32 33 53 Find approximate midpoint

  19. Data Structures Using C++ 2E Binary Search Example: sorted array of integer keys. Target=7. [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 0 ] [ 1 ] 3 6 7 11 32 33 53 Target = key of midpoint? NO.

  20. Data Structures Using C++ 2E Binary Search Example: sorted array of integer keys. Target=7. [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 0 ] [ 1 ] 3 6 7 11 32 33 53 Target < key of midpoint? NO.

  21. Data Structures Using C++ 2E Binary Search Example: sorted array of integer keys. Target=7. [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 0 ] [ 1 ] 3 6 7 11 32 33 53 Target > key of midpoint? YES.

  22. Data Structures Using C++ 2E Binary Search Example: sorted array of integer keys. Target=7. [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 0 ] [ 1 ] 3 6 7 11 32 33 53 Search for the target in the area after midpoint.

  23. Data Structures Using C++ 2E Binary Search Example: sorted array of integer keys. Target=7. [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 0 ] [ 1 ] 3 6 7 11 32 33 53 Find approximate midpoint. Is target = midpoint key? YES.

  24. Binary Search (recursive) void search(const int a[ ], size_t first, size_t size, int target, bool& found, size_t& location) { size_t middle; if(size == 0) found = false; else { middle = first + size/2; if(target == a[middle]){ location = middle; found = true; } else if (target < a[middle]) // target is less than middle, so search subarray before middle search(a, first, size/2, target, found, location); else // target is greater than middle, so search subarray after middle search(a, middle+1, (size-1)/2, target, found, location); } }

  25. Binary Search (iterative)

  26. Data Structures Using C++ 2E Relation to Binary Search Tree Array of previous example: 3 6 7 11 32 33 53 Corresponding complete binary search tree 11 6 33 32 53 3 7

  27. Data Structures Using C++ 2E Search for target = 7 Find midpoint: 3 6 7 11 32 33 53 Start at root: 11 6 33 32 53 3 7

  28. Data Structures Using C++ 2E Search for target = 7 Search left subarray: 3 6 7 11 32 33 53 Search left subtree: 11 6 33 32 53 3 7

  29. Data Structures Using C++ 2E Search for target = 7 Find approximate midpoint of subarray: 3 6 7 11 32 33 53 Visit root of subtree: 11 6 33 32 53 3 7

  30. Data Structures Using C++ 2E Search for target = 7 Search right subarray: 3 6 7 11 32 33 53 Search right subtree: 11 6 33 32 53 3 7

  31. Data Structures Using C++ 2E Binary Search: Analysis Worst case complexity? What is the maximum depth of recursive calls in binary search as function of n? Each level in the recursion, we split the array in half (divide by two). Therefore maximum recursion depth is floor(log2n) and worst case = O(log2n). Average case is also = O(log2n).

  32. Insertion into an Ordered List • After insertion: resulting list must be ordered • Find place in the list to insert item • Use algorithm similar to binary search algorithm • Slide list elements one array position down to make room for the item to be inserted • Insert the item • Use function insertAt (class arrayListType) • An unsuccessful search first, to find the place to insert • Then call insertAt () to insert. Data Structures Using C++ 2E 32

  33. TABLE 9-4 Number of comparisons for a list of length n Insertion into an Ordered List (cont’d.) • Can also override function seqSearch • Perform sequential search on an ordered list • Takes into account that elements are ordered Data Structures Using C++ 2E 33

  34. Lower Bound on Comparison-Based Search Algorithms • Comparison-based search algorithms • Search list by comparing target element with list elements • Sequential search: order n • Binary search: order log2n Data Structures Using C++ 2E 34

  35. Lower Bound on Comparison-Based Search Algorithms (cont’d.) • Devising a search algorithm with order less than log2n • Obtain lower bound on number of comparisons • Cannot be comparison based Data Structures Using C++ 2E 35

  36. Lower Bound on Comparison-Based Search Algorithms (cont’d.) • Proof is based on the decision tree model: • Visualization of the execution of a comparison-based algorithm (e.g., search or sort). • The following is the decision tree for binary search • We will revisit this topic after the sorting lectures. 36

  37. Can we do better, if not based on comparisons? • For example: O(1)? • Yes, using Hashing. Data Structures Using C++ 2E 37

More Related