Sequential search

PROBLEM
Given an array A.1,...,A.N and target value V. Search routine should return an index of V in A. if V is present in the array, and N+1 otherwise.

ALGORITHM
This algorithm takes about N steps for an unsuccessful search and about N/2 steps, on the average, for a successul search.

IMPLEMENTATION
Unit: internal function
 
Global variables: ascending sequence of numerical values A.1,...,A.N
 
Parameters: positive integer N, numerical value V
 
Returns: the index of V in A. if V is present in the array, and N+1 otherwise
 


SEQUENTIAL_SEARCH: procedure expose A.
parse arg N, V
Np1 = N + 1; A.Np1 = V
do J = 1 until A.J = V; end
return J

 

CONNECTIONS

Literature
Sedgewick R. Algorithms
Addison-Wesley, Reading, Massachusetts, 1984


cover contents index main page

last modified 8th August 2001
Copyright 2000-2001 Vladimir Zabrodsky
Czech Republic.

mail