You have two methods that I am aware of (ref: http://www2.cs.siu.edu/~mengxia/Courses%20PPT/220/carrano_ppt08.ppt):
Recursive (pseudocode)
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]
Efficiency
May be O(log n) though I have not tried it.
Sequential Search (pseudocode)
public boolean contains(Object anEntry)
{
boolean found = false;
for (int index = 0; !found && (index < length); index++) {
if (anEntry.equals(entry[index]))
found = true;
}
return found;
}
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)
I am not aware of an O(log n) search algorithm unless it is sorted.