Look at each element of array in range [first, last) until the target
is found or run out of elements. Return index of target or last.
|
Objects |
Type |
Kind |
Movement |
Name |
|
array of values |
int[ ] |
varying |
received |
arr |
|
index of lower bound |
int |
varying |
received |
first |
|
index of upper bound |
int |
varying |
received |
last |
|
search target |
int |
varying |
received |
target |
|
index of target |
int |
varying |
returned |
position |
- For position from first to last-1 do
- 1.1
- If arr[position] = target then
- 1.1.1 Return position
- Return last
// Precondition: first <= last
int LinearSearch (const int arr[], int first, int last, int target)
{
// scan indices from first <= position < last for a match
for (int position = first; position < last; position++)
if (arr[position] == target) // found target
return position;
return last; // target not found
} // end LinearSearch
Converted using latex2html on Tue Jan 31 21:56:40 CST 2006