Search - CIS 1068 Program Design and Abstraction

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
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
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays.
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Analysis of Algorithm.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
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.
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
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.,
Searching Searching: –Mainly used for: Fetching / Retrieving the Information such as, –Select query on a database. –Important thing is: Retrieval of Information.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
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.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
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.
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.
SEARCHING.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found.
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Data Structure Introduction.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: Tel 3049.
CS261 Data Structures Ordered Bag Dynamic Array Implementation.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
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.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
CS 150: Analysis of Algorithms. Goals for this Unit Begin a focus on data structures and algorithms Understand the nature of the performance of algorithms.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Sorting - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 13/14/2016.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline 1 Introduction 2 Arrays 3Declaring Arrays 4Processing Array Contents 5 Multiple-Subscripted.
COP 3503 FALL 2012 Shayan Javed Lecture 15
Introduction to Search Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
Sorting and Searching -- Introduction
Presentation transcript:

Search - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University 1050 Wachman Hall, Main Campus Email: zhen.jiang@temple.edu 4/19/2017

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 4/19/2017

The Search Problem Considering e.g., String str= “hello”; 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) 4/19/2017

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 4/19/2017

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/19/2017

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

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 4/19/2017

# 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 4/19/2017

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

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! 4/19/2017

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 4/19/2017

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. 4/19/2017

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. 4/19/2017

29 -15 -7 -6 -2 8 10 29 31 40 left mid left mid right 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: D[mid] = key! -15 -7 -6 -2 8 10 29 31 40 left mid left mid right 4/19/2017

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 4/19/2017

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); } 4/19/2017

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)); } 4/19/2017

Implement a binary search method in your Business class 4/19/2017

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 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 4/19/2017

Big O notation Analyzing algorithms/processes for efficiency Fast? T(n) = O(n2) tells the order of n2 time complexity. n∞ T(n) = O(g(n)) c1g(n) ≤ T(n) ≤ c2g(n) Linear search T(n) and binary search T(log n) 4/19/2017

Little O notation T(n) = o(n2) tells that T grows much slower than n2. T(n) = o(g(n)) 4/19/2017

Code: Output: n=keyboard.nextInt(); // try 5! for (int i = 1; i <= n; i++) { for (int j = 1; j <= 10; j++) { System.out.print((i * j) + " "); } System.out.println(); Output: 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 4/19/2017

Code: Output: ****** * * n=keyboard.nextInt(); // try 6! for (i = 1; i<=n; i++) System.out.print(“*”); System.out.println(“”); for (i = 1; i <= n-2; i++) { System.out.print(“*”); for (int j = 1; j <= n-2; j++) System.out.print(“ ”); System.out.println(“*”); } Output: ****** * * 4/19/2017

Code: Output: n=keyboard.nextInt(); // try 6! for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); Output: * ** *** **** ***** ****** 4/19/2017

Java Mechanics in Java 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); 4/19/2017

Summary 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 ( ) 4/19/2017