searching chapter 16 slides by steve armstrong letourneau university longview, tx 2007, prentice...
Post on 21-Dec-2015
219 views
TRANSCRIPT
Searching
Chapter 16
Slides by Steve ArmstrongLeTourneau University
Longview, TX2007,Prentice Hall
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
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
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.
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
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
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
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
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 implementation
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.
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 case O(1) Locate desired item first
• Worst case O(n) Must look at all the items
• Average case O(n) Must look at half the items O(n/2) is still O(n)
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.
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.
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 for binary search of a range of an array
• Note version which checks for existence of the desired item
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;
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
Click to view Java version of method binarySearch in
context
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:
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 case O(1) Locate desired item first
• Worst case O(log n) Must look at all the items
• Average case O(log n)
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.
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 search
• View recursive version of sequential search Note method contains which calls it
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 case O(1) Locate desired item first
• Worst case O(n) Must look at all the items
• Average case O(n) Must look at half the items O(n/2) is just O(n)
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.
Note: Binary search of a chain of linked nodes is impractical.
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