Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved....
-
Upload
malcolm-griffin-gaines -
Category
Documents
-
view
214 -
download
0
Transcript of Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved....
![Page 1: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/1.jpg)
Searching
Chapter 18
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Data Structures and Abstractions with Java, 4e Frank Carrano
![Page 2: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/2.jpg)
The Problem
FIGURE 18-1 Searching is an everyday occurrence
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 3: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/3.jpg)
Iterative Sequential Search of an Unsorted Array
Using a loop to search for a specific valued entry.
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 4: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/4.jpg)
Iterative Sequential Search of an Unsorted Array
FIGURE 18-2 An iterative sequential search of an
array that (a) finds its target
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 5: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/5.jpg)
Iterative Sequential Search of an Unsorted Array
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
FIGURE 18-2 An iterative sequential search of an array that (b) does not find its target
![Page 6: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/6.jpg)
Recursive Sequential Search of an Unsorted Array
Pseudocode of the logic of our recursive algorithm.
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 7: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/7.jpg)
Recursive Sequential Search of an Unsorted Array
Method that implements this algorithm will need parameters first and last.
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 8: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/8.jpg)
Recursive Sequential Search of an Unsorted Array
FIGURE 18-3 A recursive sequential search of an array that (a) finds its
target;
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 9: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/9.jpg)
Recursive Sequential Search of an Unsorted Array
FIGURE 18-3 A recursive sequential search of an array that (b) does not
find its target
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 10: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/10.jpg)
Recursive Sequential Search of an Unsorted Array
FIGURE 18-3 A recursive sequential search of an array that (b) does not
find its target
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 11: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/11.jpg)
Efficiency of a Sequential Search of an Array
The time efficiency of a sequential search of an array.
•Best case O(1)
•Worst case: O(n)
•Average case: O(n)
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 12: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/12.jpg)
Sequential Search of a Sorted Array
FIGURE 18-4 Coins sorted by their mint datesNote: sequential search can be
more efficient if the data is sorted
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 13: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/13.jpg)
Binary Search of a Sorted Array
FIGURE 18-5 Ignoring one half of the data when the data is sorted
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 14: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/14.jpg)
Binary Search of a Sorted Array
First draft of an algorithm for a binary search of an array
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 15: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/15.jpg)
Binary Search of a Sorted Array
Use parameters and make recursive calls look more like Java
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 16: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/16.jpg)
Binary Search of a Sorted Array
Refine the logic a bit, get a more complete algorithm
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 17: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/17.jpg)
Binary Search of a Sorted Array
FIGURE 18-6 A recursive binary search of a sorted
array that (a) finds its target;
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 18: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/18.jpg)
Binary Search of a Sorted Array
FIGURE 18-6 A recursive binary search of a sorted array that (b) does not find its target
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 19: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/19.jpg)
Binary Search of a Sorted Array
FIGURE 18-6 A recursive binary search of a sorted array that (b) does not find its target
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 20: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/20.jpg)
Binary Search of a Sorted Array
Implementation of the method binarySearch
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 21: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/21.jpg)
Java Class Library: The Method binarySearch
Static method binarySearchwith the above specification:
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 22: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/22.jpg)
Efficiency of a Binary Search of an Array
The time efficiency of a binary search of an array
Best case: O(1) Worst case: O(log n) Average case: O(log n)
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 23: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/23.jpg)
Iterative Sequential Search of an Unsorted Chain
FIGURE 18-7 A chain of linked nodes that contain the entries in a list
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 24: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/24.jpg)
Iterative Sequential Search of an Unsorted Chain
Implementation of iterative search
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 25: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/25.jpg)
Recursive Sequential Search of an Unsorted Chain
Implementation of the method search
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 26: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/26.jpg)
Efficiency of a Sequential Search of a Chain
The time efficiency of a sequential search of a chain of linked nodes
Best case: O(1) Worst case: O(n) Average case: O(n)
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 27: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/27.jpg)
Searching a Sorted Chain
Similar to sequentially searching a sorted array.Implementation of contains.
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 28: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/28.jpg)
Binary Search of a Sorted Chain
• To find the middle of the chain you must traverse the whole chain
• Then must traverse one of the halves to find the middle of that half
• Conclusion Hard to implement Less efficient than sequential search
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 29: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/29.jpg)
Choosing between Sequential Search and Binary Search
FIGURE 18-8 The time efficiency of searching, expressed in Big Oh notation
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 30: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/30.jpg)
Choosing between Iterative Search and Recursive Search
• Can save some time and space by using iterative version of a search
• Using recursion will not require much additional space for the recursive calls
• Coding binary search recursively is somewhat easier
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 31: Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.](https://reader036.fdocuments.net/reader036/viewer/2022081519/56649f1e5503460f94c35891/html5/thumbnails/31.jpg)
End
Chapter 18
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.