Searching - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Searching

Description:

Iterative Sequential Search. Recursive Sequential Search ... private boolean search(Node current, Object desiredItem) { boolean found; if (current = = null) ... – PowerPoint PPT presentation

Number of Views:57
Avg rating:3.0/5.0
Slides: 22
Provided by: steve1789
Category:

less

Transcript and Presenter's Notes

Title: Searching


1
Searching
  • Chapter 16

2
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
  • 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

3
The Problem
Fig. 16-1 Searching is an every day occurrence.
4
Searching an Unsorted Array
  • A method that uses a loop to search an array.

public boolean contains(Object anEntry) boolean
found false for (int index 0 !found
(index lt length) index) if
(anEntry.equals(entryindex)) found
true // end for return found // end
contains
5
Searching an Unsorted Array
Fig. 16-2 An iterative sequential search of an
array that (a) finds its target (b) does not
find its target
6
Searching an Unsorted Array
  • Pseudocode for a recursive algorithm to search an
    array.

Algorithm to search afirst through alast for
desiredItemif (there are no elements to
search) return falseelse if (desiredItem equals
afirst) return trueelse return the result of
searching afirst1 through alast
7
Searching an Unsorted Array
Fig. 16-3 A recursive sequential search of an
array that (a) finds its target (b) does not
find its target.
8
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)

9
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.

10
Binary Search of Sorted Array
Fig. 16-5 Ignoring one-half of the data when the
data is sorted.
11
Binary Search of Sorted Array
  • Algorithm for a binary search

Algorithm binarySearch(a, first, last,
desiredItem)mid (first last)/2 //
approximate midpointif (first gt last) return
falseelse if (desiredItem equals amid) return
trueelse if (desiredItem lt amid) return
binarySearch(a, first, mid-1, desiredItem)else
// desiredItem gt amid return binarySearch(a,
mid1, last, desiredItem)
12
Binary Search of Sorted Array
Fig. 16-6 A recursive binary search of a sorted
array that (a) finds its target
13
Binary Search of Sorted Array
Fig. 16-6 A recursive binary search of a sorted
array that (b) does not find its target.
14
Java Class Library The Method binarySearch
  • The class Arrays in java.util defines versions of
    a static method with following specification

/ Task Searches an entire array for a given
item. _at_param array the array to be searched
_at_param desiredItem the item to be found in the
array _at_return index of the array element that
equals desiredItem otherwise returns
-belongsAt-1, where belongsAt is the index of
the array element that should contain
desiredItem /public static int
binarySearch(type array, type desiredItem)
15
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)

16
Iterative Sequential Search of an Unsorted Chain
Fig. 16-7 A chain of linked nodes that contain
the entries in a list.
17
Iterative Sequential Search of an Unsorted Chain
  • Implementation of iterative sequential search

public boolean contains(Object anEntry) boolean
found false Node currentNode
firstNode while (!found (currentNode !
null)) if (anEntry.equals(currentNode.getData()
)) found true else currentNode
currentNode.getNextNode() // end
while return found // end contains
18
Recursive Sequential Search of an Unsorted Chain
  • Recursive search method

/ Task Recursively searches a chain of nodes
for desiredItem, beginning with the node that
current references. /private boolean
search(Node current, Object desiredItem) boolean
found if (current null) found
false else if (desiredItem.equals(current.getDat
a())) found true else found
search(current.getNextNode(), desiredItem) retur
n found // end search
19
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)

20
Searching a Sorted Chain
  • Method to search a sorted chain

public boolean contains(Object anEntry) boolean
found false Comparable entry
(Comparable)anEntry Node currentNode
firstNode while ( (currentNode ! null)
(entry.compareTo(currentNode.getData()) gt 0)
) currentNode currentNode.getNextNode()
// end while if ( (currentNode ! null)
entry.equals(currentNode.getData())
) found true // end if return found
// end contains
Note Binary search of a chain of linked nodes is
impractical.
21
Choosing a Search Method
  • Iterative search saves time, memory over
    recursive search
Write a Comment
User Comments (0)
About PowerShow.com