Presentation is loading. Please wait.

Presentation is loading. Please wait.

Adapted from Pearson Education, Inc.

Similar presentations


Presentation on theme: "Adapted from Pearson Education, Inc."— Presentation transcript:

1 Adapted from Pearson Education, Inc.
Searching Chapter 18 Adapted from Pearson Education, Inc.

2 Contents Searching an Unsorted Array Searching a Sorted Array
An Iterative Sequential Search A Recursive Sequential Search The Efficiency of a Sequential Search of an Array Searching a Sorted Array A Sequential Search A Binary Search of a Sorted Array The Efficiency of a Binary Search of an Array Searching an Unsorted Chain An Iterative Sequential Search A Recursive Sequential Search The Efficiency of a Sequential Search of a Chain Searching a Sorted Chain A Sequential Search A Binary Search of a Sorted Chain Choosing a Search Method Java Class Library: The Method binarySearch Adapted from Pearson Education, Inc.

3 Contents Searching Unsorted / Sorted Arrays
Searching Unsorted / Sorted Chains Using Iterative/Recursive Sequential Search Binary Search Choosing a Search Method The Efficiency of Searching methods Adapted from Pearson Education, Inc.

4 Objectives Search an array by using a sequential search
Search an array by using a binary search Search a chain of linked nodes sequentially Describe time efficiency of a search Adapted from Pearson Education, Inc.

5 Recursive Sequential Search of an Unsorted Array – Idea & Example 1
Basic idea: Check if the item you want is the first one If yes, you are done If not, disregard that item and focus on the remaining items Repeat the same until… ?? Adapted from Pearson Education, Inc.

6 Recursive Sequential Search of an Unsorted Array – Example 2
Adapted from Pearson Education, Inc.

7 Recursive Sequential Search of an Unsorted Array - Implementation
/** Searches the list for anEntry. */ public boolean contains (T anEntry) { return search (0, length - 1, anEntry); } // end contains /** Searches list[first] through list[last] for desiredItem. @param first, last two integers >= 0 and < length @param desiredItem the object to be found @return true if desiredItem is found */ private boolean search (int first,int last,T desiredItem) { boolean found; if (first > last) found = false; // nothing left to search else if (desiredItem.equals (list [first])) found = true; else found = search (first + 1, last, desiredItem); return found; } // end search Trace it to search for 8 in this List = { 9, 5, 8, 4, 7 } How often is this called at most? Adapted from Pearson Education, Inc.

8 Iterative Sequential Search of a Unsorted Array vs. Sorted Array
What is the growth-rate? Is the growth-rate different? Unsorted Array Sorted Array public boolean contains (T anEntry) { for (int index = 0 ; index < numberOfEntries; index++) if(anEntry.equals (list [index])) return true; } // end for return false; } // end contains public boolean contains (T anEntry) { int comp; for (int index = 0 ; index < numberOfEntries; index++) { comp = anEntry.compareTo (list [index])); if(comp == 0) return true; else if(comp < 0) return false; } // end for } // end contains Adapted from Pearson Education, Inc.

9 Activity – Guess the number 
Higher / Lower Correct / Incorrect P1: Choose a number between P2: Start guessing P1: answer (Hi / Lo) P3: keep a log: Write down guess Write down answer P1: Choose a number between 1-100 P2: Start guessing P1: answer ( / ) P3: keep a log: Write down guess Write down answer Compare number of guesses until correct answer was reached Adapted from Pearson Education, Inc.

10 Binary Search of a Sorted Array – Idea & Example
Basic idea: Check if the item you want is the middle one If yes, you are done If not, check if the item is less than the middle if yes, repeat in lower half of the remaining array (disregard upper) if not, repeat in upper half of the remaining array (disregard lowe Repeat the same until…?? Adapted from Pearson Education, Inc.

11 Binary Search of a Sorted Array – Implementation
public boolean contains (T anEntry) { return binarySearch (0, length - 1, anEntry); } // end contains private boolean binarySearch (int first, int last, T desiredItem) { boolean found; int mid = (first + last) / 2; if (first > last) found = false; else if (desiredItem.equals (list [mid])) found = true; else if (desiredItem.compareTo (list [mid]) < 0) found = binarySearch (first, mid - 1, desiredItem); else found = binarySearch (mid + 1, last, desiredItem); return found; } // end binarySearch Trace it to search for 8 in this List = { 2, 4, 5, 6, 8, 10, 12, 15, 18, 21, 24, 26} How often are these called at most? Adapted from Pearson Education, Inc.

12 Recursive Sequential Search of an Unsorted Chain – Self-Study
/** Searches the list for anEntry. */ public boolean contains (T anEntry) { return search (firstNode, anEntry); } // end contains /** Recursively searches a chain of nodes for desiredItem, beginning with the node that current references. */ private boolean search (Node current, T desiredItem) { boolean found; if (current == null) found = false; else if (desiredItem.equals(current.data)) found = true; else found = search (current.next, desiredItem); return found; } // end search Trace it to search for 8 in this chain = { 9, 5, 8, 4, 7 } How often is this called at most? Adapted from Pearson Education, Inc.

13 Iterative Sequential Search of an Unsorted Chain vs. Sorted Chain
What is the growth-rate? Is the growth-rate different? Unsorted Chain Sorted Chain public boolean contains (T anEntry) { Node current = firstNode; while (current != null) if (anEntry.equals (current.data)) return true; current = current.next; } // end while return false; } // end contains public boolean contains (T anEntry) { int comp; Node current = firstNode; while (current!=null) { comp = anEntry.compareTo (current.data)); if(comp == 0) return true; else if(comp < 0) return false; current = current.next; }// end while } // end contains Adapted from Pearson Education, Inc.

14 Iterative Sequential Search of a Sorted Array vs. Sorted Chain
public boolean contains (T anEntry) { int comp; for (int index = 0 ; index < numberOfEntries; index++) { comp = anEntry.compareTo (list [index])); if(comp == 0) return true; else if(comp < 0) return false; } // end for } // end contains public boolean contains (T anEntry) { int comp; Node current = firstNode; While (current!=null) { comp = anEntry.compareTo (current.data)); if(comp == 0) return true; else if(comp < 0) return false; current = current.next; }// end while } // end contains Adapted from Pearson Education, Inc.

15 Binary Search of a Sorted Chain
Recall finding middle element in an array is easy: mid = first + (last - first) / 2 Finding middle element of sorted chain more difficult Must traverse the chain to middle Less efficient than sequential search What would the Big Oh be? Adapted from Pearson Education, Inc.

16 Choosing a Search Method
Sequential search Suitable for sorted and unsorted data Suitable for array and chain Suitable for smaller collections Objects must have appropriate equals method Binary search Can NOT be used on unsorted data Unsuitable for chain Suitable for larger collections Objects must have appropriate compareTo method In both cases ask, “Search how often?” Adapted from Pearson Education, Inc.

17 The time efficiency of searching
Adapted from Pearson Education, Inc.

18 Adapted from Pearson Education, Inc.
End Chapter 18 Adapted from Pearson Education, Inc.


Download ppt "Adapted from Pearson Education, Inc."

Similar presentations


Ads by Google