Sunteți pe pagina 1din 28

Week 11: Searching

STIA2024
Data Structures & Algorithm
Analysis

1
Learning Objective
 To search an array by using a sequential
search,
 To search an array by using binary tree,

 To search chain of linked nodes sequentially,


and
 To determine the efficiency of a search.
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
The Problem

Fig. 1: Searching is an every day occurrence.


Searching an Unsorted Array

 A sequential search of the list compares


the desired item (target) with the first
entry in the list, the second entry in the
list, and so on until it either locates the
desired entry or looks all the entries
without success.
 Searching can be implement either
iteratively or recursively.
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 < length); index++)
{ if (anEntry.equals(entry[index]))
found = true;
} // end for
return found;
} // end contains
Searching an Unsorted Array

Fig. 2: An iterative sequential search of an array that (a) finds its


target; (b) does not find its target
Searching an Unsorted Array

 Pseudocode for a recursive algorithm to


search an array.

Algorithm to search a[first] through a[last] for desiredItem


if (there are no elements to search)
return false
else if (desiredItem equals a[first])
return true
else
return the result of searching a[first+1] through a[last]
Searching an Unsorted Array

 A sequential search of an array begin by


looking at first entry in an array.
 If that entry is the desired one, end the
search.
 Otherwise search rest of the array.
Searching an Unsorted Array

Fig. 3: A recursive sequential search of an


array that (a) finds its target; (b) does not find
its target.
Searching an Unsorted Array

 A method to search an array recursively.

public boolean search(int first, int last, Object desiredItem)


if (first > last)
return false
else if (desiredItem.equals (entry[first]))
return true
else
return search(first +1, last, desiredItem)
Efficiency of a Sequential Search

 Best case O(1)


 Locate desired item first in the array.
 Worst case O(n)
 Must look at all the items (search the
entire array: either at the end of an
array or not find it at all)
 Average case O(n)
 Must look at half the items
 O(n/2) is still O(n)
Searching a Sorted Array

 A sequential search can be more


efficient if the data is sorted

Fig. 4: Coins sorted by their mint dates.


Searching a Sorted Array

 If an array is sorted into either ascending


or descending order, we can use the
previous ideas to revise the sequential
search.
 This modified search can determine that
an item does not occur in an array faster
than sequential search of an unsorted
array.
Binary Search of Sorted Array

 When the array contains many elements,


a sequential search of sorted array can
be time-consuming.
  a faster search method – called as
binary search.
 Binary search find the middle item, then
decide whether to look in the left half or
in the right half. If decide to look in one
half, ignore the other one half. Repeat
the process on this half.
Binary Search of Sorted Array

Fig. 5: Ignoring one-half of the data when the


data is sorted.
Binary Search of Sorted Array

 Algorithm for a binary search

Algorithm binarySearch(a, first, last, desiredItem)


mid = (first + last)/2 // approximate midpoint
if (first > last)
return false
else if (desiredItem equals a[mid])
return true
else if (desiredItem < a[mid])
return binarySearch(a, first, mid-1, desiredItem)
else // desiredItem > a[mid]
return binarySearch(a, mid+1, last, desiredItem)
Binary Search of Sorted Array

Fig. 6: A recursive binary search of a sorted array


that (a) finds its target;
Binary Search of Sorted Array

Fig. 6: A recursive binary search of a sorted array that (b)


does not find its target.
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.
* @param array the array to be searched
* @param desiredItem the item to be found in the array
* @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);
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)
Iterative Sequential Search of an Unsorted
Chain

Fig. 16-7 A chain of linked nodes that contain the entries


in a list.
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
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.getData()))
found = true;
else
found = search(current.getNextNode(), desiredItem);
return found;
} // end search
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)
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()) > 0) )
{ currentNode = currentNode.getNextNode();
} // end while
if ( (currentNode != null) &&
entry.equals(currentNode.getData()) )
{ found = true;
} // end if
return found;
Note: Binary search of a chain
} // end contains
of linked nodes is impractical.
Choosing a Search Method

Best Average Worst


Case Case Case
Sequential search
(unsorted data) O(1) O(n) O(n)

Sequential search
(sorted data) O(1) O(n) O(n)

Binary Search
(sorted array) O(1) O(log n) O(log n)

 Iterative search saves time, memory over


recursive search
References
 Data Structures and Abstractions with Java . Authors: Frank M. Carrano
& Walter Savitch . Chapter 28
 Data Structures with Java . Authors : Hubbard J.R. & Huray A. . Chapter
13

S-ar putea să vă placă și