Download presentation
Presentation is loading. Please wait.
Published byKimberly Hampton Modified over 9 years ago
1
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms
2
Data Structures Using C++ 2E2 Objectives Learn 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
3
Data Structures Using C++ 2E3 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
4
Data Structures Using C++ 2E4 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
5
seqSearch in array Data Structures Using C++ 2E
6
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
7
Data Structures Using C++ 2E7 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
8
Data Structures Using C++ 2E8 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
9
Data Structures Using C++ 2E9 Sequential Search Analysis (cont’d.) Determining the average number of comparisons (cont’d.)
10
Data Structures Using C++ 2E10 Binary Search Performed only on ordered lists Uses divide-and-conquer technique 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]
11
Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target=7. 36711323353 [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ]
12
Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target=7. 36711323353 [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Find approximate midpoint
13
Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target=7. 36711323353 [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Is 7 = midpoint key? NO.
14
Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target=7. 36711323353 [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Is 7 < midpoint key? YES.
15
Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target=7. 36711323353 [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Search for the target in the area before midpoint.
16
Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target=7. 36711323353 [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Find approximate midpoint
17
Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target=7. 36711323353 [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Target = key of midpoint? NO.
18
Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target=7. 36711323353 [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Target < key of midpoint? NO.
19
Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target=7. 36711323353 [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Target > key of midpoint? YES.
20
Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target=7. 36711323353 [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Search for the target in the area after midpoint.
21
Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target=7. 36711323353 [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Find approximate midpoint. Is target = midpoint key? YES.
22
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); }
23
Binary Search (iterative)
24
Data Structures Using C++ 2E Relation to Binary Search Tree Corresponding complete binary search tree 36711323353 3 6 7 11 32 33 53 Array of previous example:
25
Data Structures Using C++ 2E Search for target = 7 Start at root: Find midpoint: 36711323353 3 6 7 11 32 33 53
26
Data Structures Using C++ 2E Search left subarray: Search for target = 7 Search left subtree: 36711323353 3 6 7 11 32 33 53
27
Data Structures Using C++ 2E Find approximate midpoint of subarray: Search for target = 7 Visit root of subtree: 36711323353 3 6 7 11 32 33 53
28
Data Structures Using C++ 2E Search right subarray: Search for target = 7 Search right subtree: 36711323353 3 6 7 11 32 33 53
29
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(log 2 n) and worst case = O(log 2 n). Average case is also = O(log 2 n).
30
Data Structures Using C++ 2E30 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.
31
Data Structures Using C++ 2E31 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 TABLE 9-4 Number of comparisons for a list of length n
32
Data Structures Using C++ 2E32 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 log 2 n
33
Data Structures Using C++ 2E33 Lower Bound on Comparison-Based Search Algorithms (cont’d.) Devising a search algorithm with order less than log 2 n –Obtain lower bound on number of comparisons Cannot be comparison based
34
34 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
35
Data Structures Using C++ 2E35 Can we do better, if not based on comparisons? For example: O(1)? Yes, using Hashing.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.