Searching Chapter 10. 2 Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
An Introduction to Sorting Chapter Chapter Contents Selection Sort Iterative Selection Sort Recursive Selection Sort The Efficiency of Selection.
An Introduction to Sorting Chapter 9. 2 Chapter Contents Selection Sort Iterative Selection Sort Recursive Selection Sort The Efficiency of Selection.
Recursion Chapter 11 Chapter 11.
Searching and Sorting I 1 Searching and Sorting 1.
Problem Solving #6: Search & Sort ICS Outline Review of Key Topics Review of Key Topics Problem 1: Recursive Binary Search … Problem 1: Recursive.
ICS201 Lecture 20 : Searching King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
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
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Chapter 8 ARRAYS Continued
Building Java Programs Chapter 13 Searching reading: 13.3.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Data Structures Using C++1 Search Algorithms Sequential Search (Linear Search) Binary Search.
Analyzing Complexity of Lists OperationSorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) Insert(
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Lecture 12. Searching Algorithms and its analysis 1.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
1 Searching and Sorting Linear Search Binary Search.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
Standard Algorithms –search for an item in an array –count items in an array –find the largest (or smallest) item in an array.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
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.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
CSC 211 Data Structures Lecture 13
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
Chapter 11 Hash Anshuman Razdan Div of Computing Studies
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 Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index.
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.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Binary search and complexity reading:
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Sorted Lists Chapter Chapter Contents Specifications for the ADT Sorted List Using the ADT Sorted List A Linked Implementation The Method add The.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Building Java Programs Chapter 12: Recursive public/private pairs Chapter 13: Searching reading: 13.3.
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
The Sequential Search (Linear Search)
An Introduction to 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.
COMP 103 Binary Search Trees.
Searching.
A Bag Implementation that Links Data
Searching and Sorting Linear Search Binary Search ; Reading p
Adapted from Pearson Education, Inc.
Lecture 14: binary search and complexity reading:
Cs212: DataStructures Lecture 3: Searching.
The Sequential Search (Linear Search)
Sorting Algorithms.
Presentation transcript:

Searching Chapter 10

2 Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of Sequential Search Searching a Sorted Array Sequential search Binary Search Java Class Library: the Method binarySearch Searching an Unsorted Chain Iterative Sequential Search Recursive Sequential Search Efficiency of Sequential Search of a Chain Searching a Sorted Chain Sequential Search Binary Search Choosing a Search Method

3 The Problem Fig. 1 Searching is an every day occurrence.

4 Searching an Unsorted Array A method that uses a loop to search an array. public boolean contains(Object anEntry) {boolean found = false; for (int index = 0; !found && (index < length); index++) {if (anEntry.equals(entry[index])) found = true; } // end for return found; } // end contains

5 Searching an Unsorted Array Fig. 2 An iterative sequential search of an array that (a) finds its target; (b) does not find its target

6 Searching an Unsorted Array Pseudocode for a recursive algorithm to search an array. Algorithm to search a[first] through a[last] for desiredItem if (there are no elements to search) return false else if (desiredItem equals a[first]) return true else return the result of searching a[first+1] through a[last]

7 Searching an Unsorted Array Fig. 3 A recursive sequential search of an array that (a) finds its target; (b) does not find its target.

8 Efficiency of a Sequential Search Best caseO(1) Locate desired item first Worst caseO(n) Must look at all the items Average caseO(n) Must look at half the items O(n/2) is still O(n)

9 Searching a Sorted Array A sequential search can be more efficient if the data is sorted Fig. 4 Coins sorted by their mint dates.

10 Binary Search of Sorted Array Fig. 5 Ignoring one-half of the data when the data is sorted.

11 Binary Search of Sorted Array Algorithm for a binary search Algorithm binarySearch(a, first, last, desiredItem) mid = (first + last)/2 // approximate midpoint if (first > last) return false else if (desiredItem equals a[mid]) return true else if (desiredItem a[mid] return binarySearch(a, mid+1, last, desiredItem)

12 Binary Search of Sorted Array Fig. 6 A recursive binary search of a sorted array that (a) finds its target;

13 Binary Search of Sorted Array Fig. 6 A recursive binary search of a sorted array that (b) does not find its target.

14 Java Class Library: The Method binarySearch The class Arrays in java.util defines versions of a static method with following specification: /** Task: Searches an entire array for a given item. array the array to be searched desiredItem the item to be found in the array index of the array element that equals desiredItem; * otherwise returns -belongsAt-1, where belongsAt is * the index of the array element that should contain * desiredItem */ public static int binarySearch(type[] array, type desiredItem);

15 Efficiency of a Binary Search Best caseO(1) Locate desired item first Worst caseO(log n) Must look at all the items Average caseO(log n)

16 Iterative Sequential Search of an Unsorted Chain Fig. 7 A chain of linked nodes that contain the entries in a list.

17 Iterative Sequential Search of an Unsorted Chain Implementation of iterative sequential search public boolean contains(Object anEntry) {boolean found = false; Node currentNode = firstNode; while (!found && (currentNode != null)) {if (anEntry.equals(currentNode.getData())) found = true; else currentNode = currentNode.getNextNode(); } // end while return found; } // end contains

18 Recursive Sequential Search of an Unsorted Chain Recursive search method /** Task: Recursively searches a chain of nodes for desiredItem, * beginning with the node that current references. */ private boolean search(Node current, Object desiredItem) {boolean found; if (current = = null) found = false; else if (desiredItem.equals(current.getData())) found = true; else found = search(current.getNextNode(), desiredItem); return found; } // end search

19 Efficiency of a Sequential Search of a Chain Best caseO(1) Locate desired item first Worst caseO(n) Must look at all the items Average caseO(n) Must look at half the items O(n/2) is just O(n)

20 Searching a Sorted Chain Method to search a sorted chain public boolean contains(Object anEntry) {boolean found = false; Comparable entry = (Comparable)anEntry; Node currentNode = firstNode; while ( (currentNode != null) && (entry.compareTo(currentNode.getData()) > 0) ) {currentNode = currentNode.getNextNode(); } // end while if ( (currentNode != null) && entry.equals(currentNode.getData()) ) {found = true; } // end if return found; } // end contains Note: Binary search of a chain of linked nodes is impractical.

21 Choosing a Search Method Iterative search saves time, memory over recursive search Best Case Average Case Worst Case Sequential search (unsorted data) O(1)O(n) Sequential search (sorted data) O(1)O(n) Binary Search (sorted array) O(1)O(log n)