Adapted from Pearson Education, Inc.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Searching and Sorting I 1 Searching and Sorting 1.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
Problem Solving #6: Search & Sort ICS Outline Review of Key Topics Review of Key Topics Problem 1: Recursive Binary Search … Problem 1: Recursive.
June Searching CE : Fundamental Programming Techniques 16 Searching1.
ICS201 Lecture 20 : Searching King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Searching. Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Searching Arrays Linear search Binary search small arrays
Searching Chapter 18 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
Chapter 8 ARRAYS Continued
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.
Chapter 16: Searching, Sorting, and the vector Type.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
CSC 211 Data Structures Lecture 13
Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
CS261 Data Structures Ordered Bag Dynamic Array Implementation.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
 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.
Chapter 16: Searching, Sorting, and the vector Type.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Searching Arrays Linear search Binary search small arrays
Using recursion for Searching and Sorting
Chapter 16: Searching, Sorting, and the vector Type
16 Searching and Sorting.
Searching.
Chapter 18: Searching and Sorting Algorithms
Big-O notation Linked lists
Lecture 14 Searching and Sorting Richard Gesick.
CSC 222: Object-Oriented Programming
Recitation 13 Searching and Sorting.
Binary Search one reason that we care about sorting is that it is much faster to search a sorted list compared to sorting an unsorted list the classic.
An Introduction to Sorting
Binary Search Trees (I)
COMP 103 Binary Search Trees.
Teach A level Computing: Algorithms and Data Structures
Searching.
A Bag Implementation that Links Data
Linear and Binary Search
Algorithm design and Analysis
searching Concept: Linear search Binary search
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
Searching: linear & binary
Searching CLRS, Sections 9.1 – 9.3.
24 Searching and Sorting.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Searching.
Searching.
Warmup Which of the 4 sorting algorithms uses recursion?
Presentation transcript:

Adapted from Pearson Education, Inc. Searching Chapter 18 Adapted from Pearson Education, Inc.

Contents Searching an Unsorted Array Searching a Sorted Array An Iterative Sequential Search A Recursive Sequential Search The Efficiency of a Sequential Search of an Array Searching a Sorted Array A Sequential Search A Binary Search of a Sorted Array The Efficiency of a Binary Search of an Array Searching an Unsorted Chain An Iterative Sequential Search A Recursive Sequential Search The Efficiency of a Sequential Search of a Chain Searching a Sorted Chain A Sequential Search A Binary Search of a Sorted Chain Choosing a Search Method Java Class Library: The Method binarySearch Adapted from Pearson Education, Inc.

Contents Searching Unsorted / Sorted Arrays Searching Unsorted / Sorted Chains Using Iterative/Recursive Sequential Search Binary Search Choosing a Search Method The Efficiency of Searching methods Adapted from Pearson Education, Inc.

Objectives Search an array by using a sequential search Search an array by using a binary search Search a chain of linked nodes sequentially Describe time efficiency of a search Adapted from Pearson Education, Inc.

Recursive Sequential Search of an Unsorted Array – Idea & Example 1 Basic idea: Check if the item you want is the first one If yes, you are done If not, disregard that item and focus on the remaining items Repeat the same until… ?? Adapted from Pearson Education, Inc.

Recursive Sequential Search of an Unsorted Array – Example 2 Adapted from Pearson Education, Inc.

Recursive Sequential Search of an Unsorted Array - Implementation /** Searches the list for anEntry. */ public boolean contains (T anEntry) { return search (0, length - 1, anEntry); } // end contains /** Searches list[first] through list[last] for desiredItem. @param first, last two integers >= 0 and < length @param desiredItem the object to be found @return true if desiredItem is found */ private boolean search (int first,int last,T desiredItem) { boolean found; if (first > last) found = false; // nothing left to search else if (desiredItem.equals (list [first])) found = true; else found = search (first + 1, last, desiredItem); return found; } // end search Trace it to search for 8 in this List = { 9, 5, 8, 4, 7 } How often is this called at most? Adapted from Pearson Education, Inc.

Iterative Sequential Search of a Unsorted Array vs. Sorted Array What is the growth-rate? Is the growth-rate different? Unsorted Array Sorted Array public boolean contains (T anEntry) { for (int index = 0 ; index < numberOfEntries; index++) if(anEntry.equals (list [index])) return true; } // end for return false; } // end contains public boolean contains (T anEntry) { int comp; for (int index = 0 ; index < numberOfEntries; index++) { comp = anEntry.compareTo (list [index])); if(comp == 0) return true; else if(comp < 0) return false; } // end for } // end contains Adapted from Pearson Education, Inc.

Activity – Guess the number  Higher / Lower Correct / Incorrect P1: Choose a number between 1-1000 P2: Start guessing P1: answer (Hi / Lo) P3: keep a log: Write down guess Write down answer P1: Choose a number between 1-100 P2: Start guessing P1: answer ( / ) P3: keep a log: Write down guess Write down answer Compare number of guesses until correct answer was reached Adapted from Pearson Education, Inc.

Binary Search of a Sorted Array – Idea & Example Basic idea: Check if the item you want is the middle one If yes, you are done If not, check if the item is less than the middle if yes, repeat in lower half of the remaining array (disregard upper) if not, repeat in upper half of the remaining array (disregard lowe Repeat the same until…?? Adapted from Pearson Education, Inc.

Binary Search of a Sorted Array – Implementation public boolean contains (T anEntry) { return binarySearch (0, length - 1, anEntry); } // end contains private boolean binarySearch (int first, int last, T desiredItem) { boolean found; int mid = (first + last) / 2; if (first > last) found = false; else if (desiredItem.equals (list [mid])) found = true; else if (desiredItem.compareTo (list [mid]) < 0) found = binarySearch (first, mid - 1, desiredItem); else found = binarySearch (mid + 1, last, desiredItem); return found; } // end binarySearch Trace it to search for 8 in this List = { 2, 4, 5, 6, 8, 10, 12, 15, 18, 21, 24, 26} How often are these called at most? Adapted from Pearson Education, Inc.

Recursive Sequential Search of an Unsorted Chain – Self-Study /** Searches the list for anEntry. */ public boolean contains (T anEntry) { return search (firstNode, anEntry); } // end contains /** Recursively searches a chain of nodes for desiredItem, beginning with the node that current references. */ private boolean search (Node current, T desiredItem) { boolean found; if (current == null) found = false; else if (desiredItem.equals(current.data)) found = true; else found = search (current.next, desiredItem); return found; } // end search Trace it to search for 8 in this chain = { 9, 5, 8, 4, 7 } How often is this called at most? Adapted from Pearson Education, Inc.

Iterative Sequential Search of an Unsorted Chain vs. Sorted Chain What is the growth-rate? Is the growth-rate different? Unsorted Chain Sorted Chain public boolean contains (T anEntry) { Node current = firstNode; while (current != null) if (anEntry.equals (current.data)) return true; current = current.next; } // end while return false; } // end contains public boolean contains (T anEntry) { int comp; Node current = firstNode; while (current!=null) { comp = anEntry.compareTo (current.data)); if(comp == 0) return true; else if(comp < 0) return false; current = current.next; }// end while } // end contains Adapted from Pearson Education, Inc.

Iterative Sequential Search of a Sorted Array vs. Sorted Chain public boolean contains (T anEntry) { int comp; for (int index = 0 ; index < numberOfEntries; index++) { comp = anEntry.compareTo (list [index])); if(comp == 0) return true; else if(comp < 0) return false; } // end for } // end contains public boolean contains (T anEntry) { int comp; Node current = firstNode; While (current!=null) { comp = anEntry.compareTo (current.data)); if(comp == 0) return true; else if(comp < 0) return false; current = current.next; }// end while } // end contains Adapted from Pearson Education, Inc.

Binary Search of a Sorted Chain Recall finding middle element in an array is easy: mid = first + (last - first) / 2 Finding middle element of sorted chain more difficult Must traverse the chain to middle Less efficient than sequential search What would the Big Oh be? Adapted from Pearson Education, Inc.

Choosing a Search Method Sequential search Suitable for sorted and unsorted data Suitable for array and chain Suitable for smaller collections Objects must have appropriate equals method Binary search Can NOT be used on unsorted data Unsuitable for chain Suitable for larger collections Objects must have appropriate compareTo method In both cases ask, “Search how often?” Adapted from Pearson Education, Inc.

The time efficiency of searching Adapted from Pearson Education, Inc.

Adapted from Pearson Education, Inc. End Chapter 18 Adapted from Pearson Education, Inc.