Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching Algorithms. The Search Problem Problem Statement: Given a set of data e.g., int [] arr = {10, 2, 7, 9, 7, 4}; and a particular value, e.g.,

Similar presentations


Presentation on theme: "Searching Algorithms. The Search Problem Problem Statement: Given a set of data e.g., int [] arr = {10, 2, 7, 9, 7, 4}; and a particular value, e.g.,"— Presentation transcript:

1 Searching Algorithms

2 The Search Problem Problem Statement: Given a set of data e.g., int [] arr = {10, 2, 7, 9, 7, 4}; and a particular value, e.g., int val = 7; Find the first index of the value in the data. e.g., return index = 2

3 The Search Problem Problem Statement, revisited: Input: A set of data (an array, ArrayList, LinkedList, …) A single data element Output: Position of the data element in the data set, or -1 if the element does not appear in the data set

4 Linear Search Algorithm # Input: Array D, integer key # Output: first index of key in D, # or -1 if not found For i = 0 to last index of D: if D[i] equals key: return i return -1

5 Linear Search for Phone Numbers # Input: Array D of Business objects, # phone number key # Output: first index where key’s phone # number matches D, or -1 if not found Business: phone # address name For i:= 0 to end of D: if D[i].phone matches key: return i return -1

6 Exercise 1. Implement a class called Business that includes fields for name, address, and phone number, plus a constructor and accessor methods. 2. Create a class called YellowPages that stores a set of Business objects in an array. 3. Write a LinearSearch method for the YellowPages class that finds the index of a Business, given its phone number.

7 Binary Search

8 What happens if our array is huge? Imagine finding a particular word on the Web (approximately 100,000,000,000 documents)  Linear search would take a long time Two common faster search techniques are:  Indexing (used on the Web and in databases)  Binary search We’ll discuss binary search because it’s simpler, but you can learn about indexing in later CIS classes

9 An Example Search Problem Imagine flipping through the Yellow Pages, looking for a pizza place near you.  It’s pretty easy – you just flip to the section for ‘P’, then look for ‘Pi’, then ‘Piz’, …, ‘Pizza’. Now imagine doing the reverse: find the name of a business given just their phone number.  What algorithm will find the number in the phone book?  Answer: you need to use (some version of) linear search! Ugh.

10 Binary Search: Normal Phone Book Use Normally, when you search the phone book, you implicitly use the fact that it’s sorted: The smallest element (alphabetically first element) appears first. Then the next smallest, … Then the biggest (alphabetically last) element. Binary search does the same thing, and it only works if your data (array) is sorted.

11 Binary Search Example -15-7-6-20810293140 29 Find key: left rightmid Step 1: Define left and right boundaries for searching Step 2: Define middle of the search region Step 3: Compare the middle with our key Comparison: D[mid] < key left Repeat! mid Comparison: D[mid] = key!

12 Binary Search Algorithm # Input: Sorted Array D, integer key # Output: first index of key in D, or -1 if not found left = 0, right = index of last element while left <= right: middle = index halfway between left, right if D[middle] matches key: return middle else if key comes before D[middle]:// b/c D is sorted right = middle -1 else: left = middle + 1 return -1

13 You guessed it: Exercise Implement a binary search method in your Business class

14 How much faster is binary search? Way, way faster  Assuming the array is already sorted But precisely how much? For an array of size: Linear search might visit:Binary search might visit: 2 4 = 1616elements4+1 = log 2 (16)+1 2 8 = 256256 elements8+1 = log 2 (256)+1 2 12 = 40964096 elements12+1 = log 2 (4096)+1 2 n = m elementsm elementsn + 1 = log 2 (m) + 1

15 Arrays.binarySearch The Java class Arrays has numerous helpful methods built in, including a binary search method: public static int binarySearch(int[] a, int key):binarySearch Searches the specified array of ints for the specified value using the binary search algorithm. Example: int index = Arrays.binarySearch(arr, 29);

16 Binary Search: What you should know 1. The requirements for it to work (array is sorted) 2. How to simulate it on an example array  That is, what sequence of indexes are compared with the key for a specific input key? 3. Write the algorithm for it 4. Advantages and Disadvantages compared with linear search 5. How to use Arrays.binarySearch()

17 Exercise Modify your Business class so that the Arrays.sort() method will work on it What are the two things you need to do?  Declare that the class implements the Comparable interface  Implement the compareTo() method Make it sort the Businesses according to phone number


Download ppt "Searching Algorithms. The Search Problem Problem Statement: Given a set of data e.g., int [] arr = {10, 2, 7, 9, 7, 4}; and a particular value, e.g.,"

Similar presentations


Ads by Google