Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching Chapter 18 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Similar presentations


Presentation on theme: "Searching Chapter 18 Copyright ©2012 by Pearson Education, Inc. All rights reserved."— Presentation transcript:

1 Searching Chapter 18 Copyright ©2012 by Pearson Education, Inc. All rights reserved

2 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

3 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

4 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

5 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

6 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

7 The Problem Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 18-1 Searching is an everyday occurrence

8 Searching an Unsorted Array Iterative search, unsorted array Copyright ©2012 by Pearson Education, Inc. All rights reserved

9 Figure 18-2 An iterative sequential search of an array that (a) finds its target; Copyright ©2012 by Pearson Education, Inc. All rights reserved

10 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

11 Recursive Sequential Search of an Unsorted Array Copyright ©2012 by Pearson Education, Inc. All rights reserved

12 Figure 18-3 A recursive sequential search of an array that (a) finds its target; Copyright ©2012 by Pearson Education, Inc. All rights reserved

13 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

14 Recursive Sequential Search of an Unsorted Array Efficiency of a sequential search of an array Copyright ©2012 by Pearson Education, Inc. All rights reserved

15 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

16 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

17 Binary Search of a Sorted Array Algorithm for binary search Copyright ©2012 by Pearson Education, Inc. All rights reserved

18 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

19 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

20 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

21 Binary Search of a Sorted Array Implementation of binarySearch Copyright ©2012 by Pearson Education, Inc. All rights reserved

22 Java Class Library: The Method binarySearch Class Arrays contains versions of static method  Note specification Copyright ©2012 by Pearson Education, Inc. All rights reserved

23 Efficiency of a Binary Search of an Array Given n elements to be searched Number of recursive calls is of order log 2 n Copyright ©2012 by Pearson Education, Inc. All rights reserved

24 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

25 Iterative Sequential Search of an Unsorted Chain Straightforward implementation Copyright ©2012 by Pearson Education, Inc. All rights reserved

26 Recursive Sequential Search of an Unsorted Chain Method search Copyright ©2012 by Pearson Education, Inc. All rights reserved

27 Recursive Sequential Search of an Unsorted Chain Public method contains  Calls method search Copyright ©2012 by Pearson Education, Inc. All rights reserved

28 Efficiency of a Sequential Search of a Chain Copyright ©2012 by Pearson Education, Inc. All rights reserved

29 Sequential Search of a Sorted Chain Similar to sequentially searching a sorted array Copyright ©2012 by Pearson Education, Inc. All rights reserved

30 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

31 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

32 Figure 18-8 The time efficiency of searching, expressed in Big Oh notation Copyright ©2012 by Pearson Education, Inc. All rights reserved

33 End Chapter 18 Copyright ©2012 by Pearson Education, Inc. All rights reserved


Download ppt "Searching Chapter 18 Copyright ©2012 by Pearson Education, Inc. All rights reserved."

Similar presentations


Ads by Google