|
A search algorithm scans a sequence once looking for a value with some desired property.
As soon as it finds a value with that property, the search stops.
If it reaches the end of the sequence without finding a value with the desired property, the search also stops.
Suppose that A is an array of size n. A boilerplate algorithm to search array A is as follows.
for(int i = 0; i < n; i++) { if(A[i] has the desired property) { break or return (as appropriate): the value has been found } } There is no value in array A with the desired property
Notice that the if-statement has no else part. If A[i] does not have the desired property, the algorithm tries the next value of i.
|