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

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data.
CSE Lecture 3 – Algorithms I
Analysis of Algorithms CS Data Structures Section 2.6.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
HST 952 Computing for Biomedical Scientists Lecture 9.
1 Lecture 23 Searching Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11: Sorting and Searching  Searching Linear Binary  Sorting.
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Search - CIS 1068 Program Design and Abstraction
1 CSC 222: Computer Programming II Spring 2005 Searching and efficiency  sequential search  big-Oh, rate-of-growth  binary search  example: spell checker.
Chapter 8 ARRAYS Continued
Chapter 5 Ordered List. Overview ● Linear collection of entries  All the entries are arranged in ascending or descending order of keys.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--WuChapter Chapter 10 Sorting and Searching.
Do Now Take out ch6 test answers – be ready to hand it in Pick a leader in each group of up to 3 students; Leader will retrieve a whiteboard, marker, and.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Building Java Programs Chapter 13 Searching reading: 13.3.
Search - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 19/23/2015.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Chapter 19 Searching, Sorting and Big O
1 Searching. 2 Searching Searching refers to the operation of finding an item from a list of items based on some key value. Two Searching Methods (1)
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
P-1 University of Washington Computer Programming I Lecture 15: Linear & Binary Search ©2000 UW CSE.
1 2. Program Construction in Java. 2.8 Searching.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
CSC 211 Data Structures Lecture 13
“Enthusiasm releases the drive to carry you over obstacles and adds significance to all you do.” – Norman Vincent Peale Thought for the Day.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: Tel 3049.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Searching and Sorting.
CS261 Data Structures Ordered Bag Dynamic Array Implementation.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Sorting and Searching. Searching  Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1).
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Data Structures Arrays and Lists Part 2 More List Operations.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
COP 3503 FALL 2012 Shayan Javed Lecture 15
Chapter 7 Single-Dimensional Arrays
Searching CSCE 121 J. Michael Moore.
Outline Late Binding Polymorphism via Inheritance
Searching and Sorting Arrays
Searching.
Presentation transcript:

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., int val = 7; Find the first index of the value in the data. e.g., return index = 2

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

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

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

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.

Binary Search

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

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.

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.

Binary Search Example 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!

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

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

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) = elements8+1 = log 2 (256) = elements12+1 = log 2 (4096)+1 2 n = m elementsm elementsn + 1 = log 2 (m) + 1

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

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

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