bool LinearSearch(dataType array[], dataType key, int size) {
int i;
bool found;
i = 0;
found = false;
while(!found && i < size) {
if (array[i] == key) {
found = true;
} else {
i++;
}
}
return found;
}
- What happens when an item is in the array?
- What happens when an item is not in an array?
- Consider the special cases, key is first item or key is last item.
- Notice, this returns the first instance of the item found in the list, what if we wanted the last instance of the item?