Consider the problem of linear search. the input is a sequence of n numbers a = ha1, a2, . . . , ani and a target value v. the output is an index i such that v = a[i] or the special value nil if v does not appear in
a.

Respuesta :

pseudocode of a possible algorithm for linear search, generally applicable to unsorted sequences/lists.
We assume that the list index starts with 1, i.e. first item of the list is a[1]=a1, and the ith item is a[i]=ai.


value:v; // define and initialize comparison variable
v:nil;     // prescribed special value
i:0         // define and initialize counter
while (i<n and v==nil) do{    // terminates while if value found, or n cycles reached
  i:i+1;
  if a[i]==value then v=i;   // index of matching value
}
// if value not found, v=nil, else v=i, the index of the list/array where found