Data Structures and Abstractions with Java 5 th
Data Structures and Abstractions with Java™ 5 th Edition Chapter 19 Searching Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
The Problem FIGURE 19 -1 Searching is an everyday occurrence Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Iterative Sequential Search of an Unsorted Array public static <T> boolean in. Array(T[] an. Array, T an. Entry) { boolean found = false; int index = 0; while (!found && (index < an. Array. length)) { if (an. Entry. equals(an. Array[index])) found = true; index++; } // end while return found; } // end in. Array Using a loop to search for a specific valued entry. Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Iterative Sequential Search of an Unsorted Array FIGURE 19 -2 a An iterative sequential search of an array Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Iterative Sequential Search of an Unsorted Array FIGURE 19 -2 b An iterative sequential search of an array Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Recursive Sequential Search of an Unsorted Array Algorithm to search a[first] through a[last] for desired. Item if (there are no elements to search) return false else if (desired. Item equals a[first]) return true else return the result of searching a[first + 1] through a[last] Pseudocode of the logic of our recursive algorithm. Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Recursive Sequential Search of an Unsorted Array /** Searches an array for an. Entry. */ public static <T> boolean in. Array(T[] an. Array, T an. Entry) { return search(an. Array, 0, an. Array. length - 1, an. Entry); } // end in. Array // Searches an. Array[first] through an. Array[last] for desired. Item. // first >= 0 and < an. Array. length. // last >= 0 and < an. Array. length. private static <T> boolean search(T[] an. Array, int first, int last, T desired. Item) { boolean found; if (first > last) found = false; // No elements to search else if (desired. Item. equals(an. Array[first])) found = true; else found = search(an. Array, first + 1, last, desired. Item); return found; } // end search Method that implements this algorithm will need parameters first and last. Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Recursive Sequential Search of an Unsorted Array FIGURE 19 -3 a A recursive sequential search of an array Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Recursive Sequential Search of an Unsorted Array FIGURE 19 -3 b A recursive sequential search of an array Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
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) Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Sequential Search of a Sorted Array FIGURE 19 -4 Coins sorted by their mint dates Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Binary Search of a Sorted Array FIGURE 19 -5 Ignoring one half of the data when the data is sorted Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Binary Search of a Sorted Array Algorithm to search a[0] through a[n − 1] for desired. Item mid = approximate midpoint between 0 and n – 1 if (desired. Item equals a[mid]) return true else if (desired. Item < a[mid]) return the result of searching a[0] through a[mid – 1] else if (desired. Item > a[mid]) return the result of searching a[mid + 1] through a[n – 1] First draft of an algorithm for a binary search of an array Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Binary Search of a Sorted Array Algorithm binary. Search(a, first, last, desired. Item) mid = approximate midpoint between first and last if (desired. Item equals a[mid]) return true else if (desired. Item < a[mid]) return binary. Search(a, first, mid − 1, desired. Item) else if (desired. Item > a[mid]) return binary. Search(a, mid + 1, last, desired. Item) Revision of binary search algorithm as method Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Binary Search of a Sorted Array Algorithm binary. Search(a, first, last, desired. Item) mid = (first + last) / 2 // Approximate midpoint if (first > last) return false else if (desired. Item equals a[mid]) return true else if (desired. Item < a[mid]) return binary. Search(a, first, mid − 1, desired. Item) else // desired. Item > a[mid] return binary. Search(a, mid + 1, last, desired. Item) Refine the logic a bit, get a more complete algorithm Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Binary Search of a Sorted Array FIGURE 19 -6 a A recursive binary search of a sorted array Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Binary Search of a Sorted Array FIGURE 19 -6 b A recursive binary search of a sorted array Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Binary Search of a Sorted Array private static <T extends Comparable<? super T>> boolean binary. Search(T[] an. Array, int first, int last, T desired. Item) { boolean found; int mid = first + (last - first) / 2; if (first > last) found = false; else if (desired. Item. equals(an. Array[mid])) found = true; else if (desired. Item. compare. To(an. Array[mid]) < 0) found = binary. Search(an. Array, first, mid - 1, desired. Item); else found = binary. Search(an. Array, mid + 1, last, desired. Item); return found; } // end binary. Search public static <T extends Comparable<? super T>> boolean in. Array(T an. Entry) { return binary. Search(an. Array, 0, an. Array. length - 1, an. Entry); } // end in. Array Implementation of the method binary. Search Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Java Class Library: The Method binary. Search /** Searches an entire array for a given item. @param array An array sorted in ascending order. @param desired. Item The item to be found in the array. @return Index of the array entry that equals desired. Item; otherwise returns –belongs. At – 1, where belongs. At is the index of the array element that should contain desired. Item. */ public static int binary. Search(type[] array, type desired. Item); Static method binary. Search specification Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
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) Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Iterative Sequential Search of an Unsorted Chain FIGURE 19 -7 A chain of linked nodes that contain the entries in a list Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Iterative Sequential Search of an Unsorted Chain public boolean contains(T an. Entry) { boolean found = false; Node current. Node = first. Node; while (!found && (current. Node != null)) { if (an. Entry. equals(current. Node. get. Data())) found = true; else current. Node = current. Node. get. Next. Node(); } // end while return found; } // end contains Implementation of iterative search in contains Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Recursive Sequential Search of an Unsorted Chain private boolean search(Node current. Node, T desired. Item) { boolean found; if (current. Node == null) found = false; else if (desired. Item. equals(current. Node. get. Data())) found = true; else found = search(current. Node. get. Next. Node(), desired. Item); return found; } // end search public boolean contains(T an. Entry) { return search(first. Node, an. Entry); } // end contains Implementation of recursive search in search Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Iterative Sequential Search of a Sorted Chain public boolean contains(T an. Entry) { Node current. Node = first. Node; while ( (current. Node != null) && (an. Entry. compare. To(current. Node. get. Data()) > 0) ) { current. Node = current. Node. get. Next. Node(); } // end while return (current. Node != null) && an. Entry. equals(current. Node. get. Data()); } // end contains Implementation of iterative search in contains Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Binary Search of a Sorted Chain • First find middle of the chain: – You must traverse the whole chain – Then traverse one of the halves to find the middle of that half • Conclusion – Hard to implement – Less efficient than sequential search Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Choosing between Iterative Search and Recursive Search Operation Sequential Best Case Average Case Search (unsorted Linked O(1) O(n) O(1) O(log n) data) Sequential Search (sorted data) Binary Search (sorted array) FIGURE 19 -8 The time efficiency of searching, expressed in Big Oh notation Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Choosing between Iterative Search and Recursive Search • Iterative Searches – Can save some time and space • Recursive Searches – Will not require much additional space for the recursive calls – Coding binary search recursively is easier Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
End Chapter 19 Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
- Slides: 28