Sequential Searching of 1-D Arrays
Subprograms that search in a sequential fashion
The following sequential search will find the smallest value in a list of
integers.
/**
find_small: integer function
storage
parameter
array: 1-d array of integer logically in only physically in out
low, high : in only integer
variable
index: integer
small: integer
end storage
**/
int find_small(int array[], int low, int high)
{
int small, index;
small = array[low];
for (index = low + 1; index <= high; index++)
{
if (array[index] < small)
small = array[index];
}
return small;
}
The following is a sequential (or linear) search which will find the
location of the first occurrence of a value
/**
find_value_location:integer function
storage
parameter
array: 1-d array of integer logically in only physically in out
low, high : in only integer
value: in only integer
variable
index: integer
location: integer
found : boolean
end storage
**/
int find_value_location(int array[], int low, int high, int value)
{
int location, found, index;
location = low - 1;
found = 0; /*0 stands for false in C*/
index = low;
while ((index <= high) && (found == 0))
{
if (array[index] == value)
{
location = index;
found = true;
}
index = index + 1;
}
return location
}