searching chapter 16 slides by steve armstrong letourneau university longview, tx 2007, prentice...
Post on 21-Dec-2015
216 views
TRANSCRIPT
![Page 1: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/1.jpg)
Searching
Chapter 16
Slides by Steve ArmstrongLeTourneau University
Longview, TX2007,Prentice Hall
![Page 2: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/2.jpg)
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
![Page 3: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/3.jpg)
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
![Page 4: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/4.jpg)
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.
![Page 5: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/5.jpg)
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
![Page 6: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/6.jpg)
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
![Page 7: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/7.jpg)
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
![Page 8: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/8.jpg)
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
![Page 9: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/9.jpg)
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.
![Page 10: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/10.jpg)
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)
![Page 11: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/11.jpg)
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.
![Page 12: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/12.jpg)
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.
![Page 13: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/13.jpg)
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
![Page 14: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/14.jpg)
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;
![Page 15: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/15.jpg)
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
![Page 16: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/16.jpg)
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:
![Page 17: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/17.jpg)
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)
![Page 18: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/18.jpg)
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.
![Page 19: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/19.jpg)
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
![Page 20: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/20.jpg)
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)
![Page 21: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/21.jpg)
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.
![Page 22: Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall](https://reader035.vdocument.in/reader035/viewer/2022081515/56649d605503460f94a40be7/html5/thumbnails/22.jpg)
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