Search here

Pages

Saturday, August 13, 2011

Linear Search

In computer science, linear search or sequential search is a method for finding a particular value in a list, that consists of checking every one of its elements, one at a time and in sequence, until the desired one is found.
           Linear search is the simplest search algorithm; it is a special case of brute-force search. Its worst case cost is proportional to the number of elements in the list; and so is its expected cost, if all list elements are equally likely to be searched for. Therefore, if the list has more than a few elements, other methods (such as binary search or hashing) will be faster, but they also impose additional requirements.
For a list with n items, the best case is when the value is equal to the first element of the list, in which case only one comparison is needed. The worst case is when the value is not in the list (or occurs only once at the end of the list), in which case n comparisons are needed.
If the value being sought occurs k times in the list, and all orderings of the list are equally likely, the expected number of comparisons is
\begin{cases} 
  n                   & \mbox{if } k = 0 \\[5pt]
  \displaystyle\frac{n + 1}{k + 1} & \mbox{if } 1 \le k \le n.
\end{cases}
For example, if the value being sought occurs once in the list, and all orderings of the list are equally likely, the expected number of comparisons is \frac{n + 1}2. However, if it is known that it occurs once, then at most n - 1 comparisons are needed, and the expected number of comparisons is
\displaystyle\frac{(n + 2)(n-1)}{2n}
(for example, for n = 2 this is 1, corresponding to a single if-then-else construct).
Either way, asymptotically the worst-case cost and the expected cost of linear search are both O(n).

Non-uniform probabilities

The performance of linear search improves if the desired value is more likely to be near the beginning of the list than to its end. Therefore, if some values are much more likely to be searched than others, it is desirable to place them at the beginning of the list.
In particular, when the list items are arranged in order of decreasing probability, and these probabilities are geometrically distributed, the cost of linear search is only O(1). If the table size n is large enough, linear search will be faster than binary search, whose cost is O(log n).
Program for linear search 
#include<stdio.h>
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
int lsearch(int a[],int,int);
void main()
{
int a[10],i,n,ele,found=0;
clrscr();
cout<<"enter the no of elements in to array";
cin>>n;
cout<<"enter elements ";
for (i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"enter the element to be searched";
cin>>ele;
found=lsearch(a,ele,n);
if(found==-1)
cout<<"element not found";
else
cout<<"element found at location "<<found;
getch();
}
int lsearch(int a[],int ele,int n)
{
int i,c=0;
for(i=0;i<n;i++)
{
if(a[i]==ele)
{
return i;
c++;
}
}
if(c==0)
return -1;
}


0 comments

Post a Comment