Vectors are simple and widely used data structures used to perform search operations in R. The simplest form of search operation performed on vectors is a linear search or sequential search. In a linear search, each element is compared sequentially within the vector, and then suitable insertions or deletions are performed. Consider an element, S
which is to be searched in an unsorted vector, V
of length n
(indexed from 1
to n
). If the element S
is not present in vector V
, then a minimum of n
comparisons are performed, and if it is present in V
at position i
, then a minimum of i
comparisons are performed. In both scenarios, the number of comparisons is linear, resulting in O(n) as the functional form of system runtime for sequential search in the worst case scenario. The following R code performs a linear search of element S
in a vector V
of length n
:
Sequential_search <- function(V,S,n)
{
i=1
present = FALSE
while(i ...