Class Exercise: Sequential Search
Objective: develop an algorithm for a sequential search in a sorted array.
Given a value to find(e.g. "cookies"), compare it to successive array element
values until a match is found, or stop looking when the place is passed
where the item would be if stored in the array. An array named list is to
have nine components initialized with values: "banana","bread","cookies",
"lettuce","oatmeal","tea","tomato","turkey","yogurt"
1. Depict list[] as a collection of cells containing the array values
given above. Define identifiers: length, an int for the number of array
components; index, an int for the position or place of an array value; and
found, a boolean to indicate whether a search item is in list[].
2. Prompt and input searchItem(e.g. "cookies"), an item to find in list[].
3. Set the variable index equal to zero(0), found to false, and length to the
number of values in list[](i.e. 9).
4. Repeat while index is less than length and (search item) NOT found:
if list element value equals the search item
set found to true,
else if list element value greater than search item
set index to length
else
increment index;
5. After the repetition completes, test whether found is true and display a
message that the item sought was found, otherwise show it was not found.
6. Create a trace table to illustrate successive iterations and corresponding
values of index, list[index], list[index] compared with the search item,
found, and the message output.
iteration index list[index] list[index]:searchItem found message
--------- ----- ----------- ---------------------- ----- --------
1 0 banana less than false
2 1 bread " "
3 2 cookies equals true item found
7. How many comparisons were required to determine "cookies" was in list[]?
8. Prepare another trace table to show the flow for a search argument that is
not in the list (e.g. cheese)
iteration index list[index] list[index]:searchItem found message
--------- ----- ----------- ---------------------- ----- -------
1 0 banana less than false
2 1 bread ...
3
.
.
.