search - cis 1068 program design and abstraction

21
Search - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: [email protected] 1 05/13/22

Upload: xannon

Post on 02-Feb-2016

72 views

Category:

Documents


0 download

DESCRIPTION

Search - CIS 1068 Program Design and Abstraction. Zhen Jiang CIS Dept. Temple University 1050 Wachman Hall, Main Campus Email: [email protected]. Table of Contents. Introduction to searching problem Problem statement Linear search algorithm Binary search Binary search algorithm - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Search - CIS 1068  Program Design and Abstraction

Search - CIS 1068 Program Design and Abstraction

Zhen Jiang

CIS Dept.

Temple University

SERC 347, Main Campus

Email: [email protected]/22/23

Page 2: Search - CIS 1068  Program Design and Abstraction

Table of Contents

Introduction to searching problem Problem statement Linear search algorithm Binary search Binary search algorithm How much fast is binary search? Search mechanics in java Summary

204/22/23

Page 3: Search - CIS 1068  Program Design and Abstraction

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 str2 = “l”;

Find 1st appearance of such a character in the original group

e.g., return str.indexOf(str2)04/22/23 3

Page 4: Search - CIS 1068  Program Design and Abstraction

Problem StatementGiven

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

04/22/23 4

Page 5: Search - CIS 1068  Program Design and Abstraction

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

04/22/23 5

Page 6: Search - CIS 1068  Program Design and Abstraction

For instance Price is right (click on this link to try) To see if you can get the price quickly…

04/22/23 6

Page 7: Search - CIS 1068  Program Design and Abstraction

Linear Search Algorithm (p541)# 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

04/22/23 7

Page 8: Search - CIS 1068  Program Design and Abstraction

# Input: Array D of Business objects,

# phone number key

# Output: first index where key’s phone

# number matches D, or -1 if not foundBusiness:

phone #addressname

For i:= 0 to end of D:if D[i].phone matches key:

return ireturn -1

04/22/23 8

Page 9: Search - CIS 1068  Program Design and Abstraction

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.

04/22/23 9

Page 10: Search - CIS 1068  Program Design and Abstraction

Binary Search Imagine finding the price among the range

up to $100,000,000 Linear search would take a long time Random guess is even worse!

04/22/23 10

Page 11: Search - CIS 1068  Program Design and Abstraction

Two common search techniques are: Indexing (used on the Web and in databases)

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’.

We can learn about indexing in later CIS classes Binary search

We’ll discuss binary search because it’s simpler

04/22/23 11

Page 12: Search - CIS 1068  Program Design and Abstraction

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.

04/22/23 12

Page 13: Search - CIS 1068  Program Design and Abstraction

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.

04/22/23 13

Page 14: Search - CIS 1068  Program Design and Abstraction

-15 -7 -6 -2 0 8 10 29 31 40

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!

04/22/23 14

Page 15: Search - CIS 1068  Program Design and Abstraction

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

04/22/23 15

Page 16: Search - CIS 1068  Program Design and Abstraction

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);}

04/22/23 16

Page 17: Search - CIS 1068  Program Design and Abstraction

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 ArrayRecursive { 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));}}

04/22/23 17

Page 18: Search - CIS 1068  Program Design and Abstraction

Implement a binary search method in your Business class

04/22/23 18

Page 19: Search - CIS 1068  Program Design and Abstraction

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:

24 = 16 16 elements 4+1 = log2(16)+1

28 = 256 256 elements 8+1 = log2(256)+1

212 = 4096 4096 elements 12+1 = log2(4096)+1

2n = m elements m elements n + 1 = log2(m) + 1

04/22/23 19

Page 20: Search - CIS 1068  Program Design and Abstraction

The Java class Arrays has numerous helpful methods built in, including a binary search method:

public static int binarySearch(int[] a, int key):

Searches the specified array of ints for the specified value using the binary search algorithm.

Example: int index = Arrays.binarySearch(arr, 29);

Java Mechanics in Java

04/22/23 20

Page 21: Search - CIS 1068  Program Design and Abstraction

The requirement for it to work (array is pre-sorted) How to simulate it on an example array

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.binarySearch ( )

2104/22/23

Summary