Simple order lookups have no requirement on the characteristics of the data table, that is, whether there is an incremental decrement feature that basically does not affect the performance of lookups. Basic death is
is to compare elements from one section of the table to the next, and return the element to the corresponding position in the table if found; otherwise, return a meaningless positional label
Knowledge
/* Order lookup, a array, n is the number of array elements to find, key is the keyword to find *
/int sequential_search (int *a, int n, int key)
{
int i = 0;
for (; i < n; i++)
{
if (a[i] = = key)
return i;
}
return 0;
}
It is worth mentioning that the idea of setting up a lookout, setting the a[0] to a monitoring whistle avoids the need to judge the subscript after each comparison of the elements
The question of cross-border, thus saving time. Of course, a[0] will not be able to store valid elements.
Here is a find implementation that takes an integer array as an example:
/* Direct order lookup with Sentinel *
/int sequential_search (int a[],int n,int key) //n number of elements, key is the element to be found
{
int i=n;
A[0]=key; A[0] is the Sentinel while
(A[i]!=key) //If there is no key in the array, it will be A[0]=key
i--;
return i; Lookup failed to return 0
}
General Catalogue
List of algorithms for each search and sort