searching chapter 18 copyright ©2012 by pearson education, inc. all rights reserved
TRANSCRIPT
Searching
Chapter 18
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Contents
• The Problem
• Searching an Unsorted Array An Iterative Sequential Search of an Unsorted
Array A Recursive Sequential Search of an
Unsorted Array The Efficiency of a Sequential Search of an
Array
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Contents
• Searching a Sorted Array A Sequential Search of a Sorted Array A Binary Search of a Sorted Array Java Class Library: The Method binarySearch
The Efficiency of a Binary Search of an Array
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Contents
• Searching an Unsorted Chain An Iterative Sequential Search of an Unsorted
Chain A Recursive Sequential Search of an
Unsorted Chain The Efficiency of a Sequential Search of a
Chain
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Contents
• Searching a Sorted Chain A Sequential Search of a Sorted Chain A Binary Search of a Sorted Chain
• Choosing a Search Method
Copyright ©2012 by Pearson Education, Inc. All rights reserved
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
Copyright ©2012 by Pearson Education, Inc. All rights reserved
The Problem
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 18-1 Searching is an everyday occurrence
Searching an Unsorted Array
• Iterative search, unsorted array
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 18-2 An iterative sequential search of an array that (a) finds its target;
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 18-2 An iterative sequential search of an array that (b) does not find its target
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Recursive Sequential Search of an Unsorted Array
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 18-3 A recursive sequential search of an array that (a) finds its target;
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 18-3 A recursive sequential search of an array that (b) does not find its target
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Recursive Sequential Search of an Unsorted Array
• Efficiency of a sequential search of an array
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Sequential Search of a Sorted Array
• Sequential search can be more efficient if the data is sorted.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 18-4 Coins sorted by their mint dates
Binary Search of a Sorted Array
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 18-5 Ignoring one half of the data when the data is sorted
Binary Search of a Sorted Array
• Algorithm for binary search
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 18-6 A recursive binary search of a sorted array that (a) finds its target;
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 18-6 A recursive binary search of a sorted array that (b) does not find its target
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Search of a Sorted Array
• Implementation of a sorted list
• Method contains that will call binarySearch
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Search of a Sorted Array
• Implementation of binarySearch
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Java Class Library: The Method binarySearch
• Class Arrays contains versions of static method Note specification
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Efficiency of a Binary Search of an Array
• Given n elements to be searched
• Number of recursive calls is of order log2 n
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Iterative Sequential Search of an Unsorted Chain
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 18-7 A chain of linked nodes that contain the entries in a list
Iterative Sequential Search of an Unsorted Chain
• Straightforward implementation
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Recursive Sequential Search of an Unsorted Chain
• Method search
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Recursive Sequential Search of an Unsorted Chain
• Public method contains Calls method search
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Efficiency of a Sequential Search of a Chain
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Sequential Search of a Sorted Chain
• Similar to sequentially searching a sorted array
Copyright ©2012 by Pearson Education, Inc. All rights reserved
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
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Choosing a Search Method
• Sequential search Suitable for smaller array Objects must have appropriate equals
method
• Binary search Suitable for larger collections Objects must have appropriate compareTo
method
• In both cases ask, “Search how often?”
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 18-8 The time efficiency of searching, expressed in Big Oh notation
Copyright ©2012 by Pearson Education, Inc. All rights reserved
End
Chapter 18
Copyright ©2012 by Pearson Education, Inc. All rights reserved