Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching algorithms can be applied to different kinds of containers. Searching algorithms can search for different things.  search for the value 4.00.

Similar presentations


Presentation on theme: "Searching algorithms can be applied to different kinds of containers. Searching algorithms can search for different things.  search for the value 4.00."— Presentation transcript:

1 Searching algorithms can be applied to different kinds of containers. Searching algorithms can search for different things.  search for the value 4.00  search for the employee named “Riley”  search for the largest value in the container What to search for?  identity equality  content equality What should the search return?  a boolean search (Is the object in the container or not?)  the position of the object (i.e. an array index or a list iterator)  the object (i.e. an array index or a list iterator) The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

2 /* pre: arr != null post:exists( k : 0 ≤ k ≤ arr.length-1 | arr[k] == z) implies arr[result] == z and forAll(k : 0 ≤ k ≤ arr.length-1 | arr[k]!=z) implies result == arr.length) */ public int indexOf(Object[] arr, Object z) { int ndx == 0; while (ndx!=arr.length && arr[ndx]!=z) { ndx++; }; return ndx; } The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

3 /* pre: stuff != null post: ( stuff.isIn(z) implies the last call to result.next() returned such an object ) and ( !stuff.isIn(z) implies result == null) */ public Iterator find(Collection stuff, E z) { Iterator it == stuff.iterator(); E temp; if (!it.hasNext()) return null; else { temp = it.next(); while (!z.equals(temp) && it.hasNext()) temp = it.next(); if (z.equals(temp)) return it; else return null; } The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

4 The previous two example searches are linear. However, a more efficient search is possible for a sorted array. Suppose you are handed a bunch of consecutive pages from a dictionary and told to search for the word “swordfish”. What is the most efficient way to perform such a search? The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

5 /* pre: forall( k : 0 ≤ k ≤ arr.length-2 | arr[k] <= arr[k+1] ) post: exists( k : 0 ≤ k ≤ arr.length-1 | z.equals(arr[k]) ) implies z.equals(arr[result]) and forAll ( k : 0 ≤ k ≤ arr.length-1 | ! z.equals(arr[k]) ) implies ! z.equals(arr[result]) */ public int binSearch(Comparable[] arr, Object z) { int lowNdx, hiNdx, midNdx; lowNdx = 0; hiNdx = arr.length - 1; while (lowNdx != hiNdx) { midNdx = (lowNdx + hiNdx) / 2; if (z.compareTo(arr[midNdx]) > 0) //z > arr[midNdx] lowNdx = midNdx + 1; else hiNdx = midNdx; } return lowNdx; } The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

6 Consider the worst case for binary search. arr.lengthcount of times the loop body is executed 42 8 16 32 64 128 N What about using binary search on an unsorted array? The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.


Download ppt "Searching algorithms can be applied to different kinds of containers. Searching algorithms can search for different things.  search for the value 4.00."

Similar presentations


Ads by Google