Search CIS 1068 Program Design and Abstraction Zhen
Search - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen. jiang@temple. edu 10/2/2020 1
Table of Contents n n n n Introduction to searching problem Problem statement Linear search algorithm Binary search algorithm How much fast is binary search? Search mechanics in java Summary 10/2/2020 2
The Search Problem Considering a sequence of characters that are contained in a string e. g. , String str= “hello”; and a particular character in another string, e. g. , String str 2 = “l”; Find 1 st appearance of such a character in the original group e. g. , return str. index. Of(str 2) 10/2/2020 3
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/position of the value in the data. e. g. , return index = 2 10/2/2020 4
Problem Statement, revisited: Input: A set of data (an array, Array. List, Linked. List, …) 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 10/2/2020 5
For instance n n Price is right (click on this link to try) To see if you can get the price quickly… 10/2/2020 6
Linear Search Algorithm (p 541) # # Input: Array D, integer key Output: first index of key in D, or -1 if not found also called sequential search For i = 0 to last index of D: if D[i] equals key: return i return -1 10/2/2020 7
# 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 10/2/2020 For i: = 0 to end of D: if D[i]. phone matches key: return i return -1 8
1. 2. 3. Implement a class called Business that includes fields for name, address, and phone number, plus a constructor and accessor methods. Create a class called Yellow. Pages that stores a set of Business objects in an array. Write a Linear. Search method for the Yellow. Pages class that finds the index of a Business, given its phone number. 10/2/2020 9
Binary Search n Imagine finding the price among the range up to $100, 000 n n Linear search would take a long time Random guess is even worse! 10/2/2020 10
n Two common search techniques are: n Indexing (used on the Web and in databases) n Imagine flipping through the Yellow Pages, looking for a pizza place near you. n n n It’s pretty easy – you just flip to the section for ‘P’, then look for ‘Pi’, then ‘Piz’, …, ‘Pizza’. We can learn about indexing in later CIS classes Binary search n 10/2/2020 We’ll discuss binary search because it’s simpler 11
n Now imagine doing the reverse: find the name of a business given just their phone number. n n 10/2/2020 What algorithm will find the number in the phone book? Answer: you need to use (some version of) linear search! Ugh. 12
n 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. n Binary search does the same thing, and it only works if your data (array) is sorted. 10/2/2020 13
Step 1: Define left and right boundaries for searching Step 2: Define middle of the search region Repeat! Step 3: Compare the middle with our key Find key: 29 Comparison: D[mid] < key Comparison: -15 -7 left 10/2/2020 -6 -2 0 8 mid left 10 D[mid] = key! 29 mid 31 40 right 14
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 10/2/2020 15
public static int bs(int [ ] n, int first, int last, int v){ int middle; if (first > last) return -1; middle = (first + last)/2; if(n[middle] = = v) return middle; else if ( n[middle] < v) return bs(n, middle+1, last, v); else return bs(n, first, middle-1, v); } 10/2/2020 16
Find out what will be the print out results of the following program and trace the position (subscript value) of each access of the array n (i. e. , the value of variable middle). public class Array. Recursive { public static void main(String [ ] args){ int [ ] n = {101, 142, 147, 189, 199, 207, 222, 234, 289, 296, 310, 319, 388, 394, 417, 429, 447, 521, 536, 600}; System. out. println( “bs(”+102+“)=”+bs(n, 0, n. length-1, 102)); System. out. println( “bs(”+296+“)=”+bs(n, 0, n. length-1, 296)); System. out. println( “bs(”+289+“)=”+bs(n, 0, n. length-1, 289)); } } 10/2/2020 17
n Implement a binary search method in your Business class 10/2/2020 18
How much faster is binary search? n Way, way faster n Assuming the array is already sorted For an array of size: n 24 = 16 Linear search might visit: Binary search might visit: But precisely how much? 16 elements 4+1 = log (16)+1 2 28 = 256 elements 8+1 = log 2(256)+1 212 = 4096 elements 12+1 = log 2(4096)+1 2 n = m elements n + 1 = log 2(m) + 1 10/2/2020 19
Java Mechanics in Java The Java class Arrays has numerous helpful methods built in, including a binary search method: public static int binary. Search(int[] a, int key): Searches the specified array of ints for the specified value using the binary search algorithm. Example: int index = Arrays. binary. Search(arr, 29); 10/2/2020 20
Summary n n n The requirement for it to work (array is pre-sorted) How to simulate it on an example array n That is, what sequence of indexes are compared with the key for a specific input key? Write the binary search algorithm for it Advantages and disadvantages compared with linear search (also called sequential search) How to use Arrays. binary. Search ( ) 10/2/2020 21
- Slides: 21