Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.

Similar presentations


Presentation on theme: "Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall."— Presentation transcript:

1 Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

2 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X 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  Efficiency of Binary Search

3 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Chapter Contents 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

4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X The Problem Fig. 16-1 Searching is an every day occurrence.

5 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Searching an Unsorted Array An iterative search of an unsorted array public boolean contains (T anEntry) { boolean found = false; for (int index = 0; !found && (index < length); index++) { if (anEntry.equals (list [index])) found = true ; } // end for return found; } // end contains

6 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Searching an Unsorted Array Fig. 16-2 An iterative sequential search of an array that (a) finds its target

7 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Searching an Unsorted Array Fig. 16-2 An iterative sequential search of an array that (b) does not find its target

8 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Recursive Sequential Search an Unsorted Array Pseudocode for a recursive algorithm to search an array. View source code of Java implementationsource code

9 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Recursive Sequential Search an Unsorted Array Fig. 16-3 A recursive sequential search of an array that (a) finds its target; (b) does not find its target.

10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X 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)

11 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Searching a Sorted Array A sequential search can be more efficient if the data is sorted Fig. 16-4 Coins sorted by their mint dates.

12 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Binary Search of Sorted Array Fig. 16-5 Ignoring one-half of the data when the data is sorted.

13 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Binary Search of Sorted Array View algorithm for a search a[0] through a[n-1]View algorithm View algorithm for binary search of a range of an arrayalgorithm for binary search Note version which checks for existence of the desired itemversion which checks

14 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Binary Search of Sorted Array Fig. 16-6 A recursive binary search of a sorted array that (a) finds its target;

15 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Binary Search of Sorted Array Fig. 16-6 A recursive binary search of a sorted array that (b) does not find its target. Click to view Java version of method binarySearch in context

16 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Java Class Library: The Method binarySearch The class Arrays in java.util defines versions of a static method with following specification:

17 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X 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)

18 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Iterative Sequential Search of an Unsorted Chain Fig. 16-7 A chain of linked nodes that contain the entries in a list.

19 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Sequential Search of an Unsorted Chain View implementation of iterative sequential searchView implementation View recursive version of sequential searchrecursive version  Note method contains which calls it

20 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X 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)

21 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Searching a Sorted Chain Method to search a sorted chain Note: Binary search of a chain of linked nodes is impractical.

22 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Choosing a Search Method Fig. 16-8 The time efficiency of searching, expressed in Big Oh notation


Download ppt "Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall."

Similar presentations


Ads by Google