1 / 21

Searching

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X. Chapter Contents. The ProblemSearching an Unsorted ArrayIterative Sequential SearchRecursive Sequential SearchEfficiency of Sequential SearchSearching a Sort

tejano
Download Presentation

Searching

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. Searching Chapter 16

    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

    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

    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

    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

    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 implementation

    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

    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 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)

    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

    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 for binary search of a range of an array Note version which checks for existence of the desired item

    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

    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

    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 case O(1) Locate desired item first Worst case O(log n) Must look at all the items Average case O(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

    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 search View recursive version of sequential search 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 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)

    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

    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

More Related