210 likes | 407 Views
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
E N D
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