Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 11 Searching. 2 Introduction Searching is the process of finding a target element among a group of items (the search pool), or determining that.

Similar presentations


Presentation on theme: "CHAPTER 11 Searching. 2 Introduction Searching is the process of finding a target element among a group of items (the search pool), or determining that."— Presentation transcript:

1 CHAPTER 11 Searching

2 2 Introduction Searching is the process of finding a target element among a group of items (the search pool), or determining that it isn't there This requires repetitively comparing the target to candidates in the search pool.

3 3 Introduction Information: Divided up into records Each record has a key, used in searching Task: Find all records with keys matching the search key Applications: Wide variety – everywhere the data is stored in files How to handle duplicate keys Keep records with distinct keys and link to all other records with the same key Return any record with that key

4 4 Linear Search A linear search simply examines each item in the search pool, one at a time, until either the target is found or until the pool is exhausted. The items in the search pool are not ordered

5 5 Linear Search

6 6 Complexity of Linear Search  (N), O(N) Successful search- about N/2 comparisons Unsuccessful search- N comparisons (1 + 2 + 3 … + N ) = (1 + N)N/2 Average number of comparisons: (1 + 2 + 3 … + N ) / N = (N+1)/2

7 7 Binary Search Based on the divide-and-conquer strategy. Algorithm Until there are elements in the array repeat: - Divide the array into two parts - Look at the middle element. If it is the sought element – return it. Else - find out to which part the sought record belongs - Repeat he procedure for that part of the file. If no record is found – report unsuccessful search.

8 8 Binary Search

9 9 Recursion in Binary Search The Binary search algorithm is recursive. Each recursive call searches a smaller portion of the search pool. The base case of the recursion is running out of viable candidates to search, which means the target is not in the search pool. When the method completes, control returns to the method that invoked it.

10 10 Complexity of Binary Search  (log(N)), O(log(N)) About Log(N ) comparisons for both successful and unsuccessful search. Why log(N)? Level 0 Level 1 Level 2 Level 3

11 11 Complexity of Binary Search N nodes at the bottom - the number of elements The root (the node at level 0) corresponds to the whole array When splitting the array into 2, we traverse a path from the root to the bottom nodes. With N nodes at the bottom, the length of the path is log(N). Disadvantage – inserting new records is expensive as the file has to be kept sorted. N/2 records to be moved on average. Duplicate keys - Search both directions

12 12 Java implementations The Comparable Interface Contains one method, compareTo, which is designed to return an integer that specifies the relationship between two objects: obj1.compareTo(obj2) Returns a number less than, equal to, or greater than 0 if obj1 is less than, equal to, or greater than obj2, respectively public class SortingandSearching


Download ppt "CHAPTER 11 Searching. 2 Introduction Searching is the process of finding a target element among a group of items (the search pool), or determining that."

Similar presentations


Ads by Google