Sections


Main-Menu

header image

Linear Search


Sequential Search or Linear Search

In linear search the elements of the array are accessed one by one to see if it is the desired element or not. The search will be unsuccessful if all the elements are accessed and the desired element is not found. Thus linear search can be defined as the technique which traverses the array in sequential manner to locate given item.
The efficiency of the technique can be known by the time taken or the number of comparison made during searching. The worst case efficiency of this technique is O(n).

Algorithm:

Assumptions: suppose  a is the array with n elements and ele is the item to be searched, lc is the location of ele in a, if search is unsuccessful then set  lc=0,

1.    [Insert item at the end of the data] set data[n+1]=ele.
2.    [Initialize the counter] set lc=1.
3.    [Search for item]
Repeat while data[lc]=!ele:
set lc = lc + 1.
4.    [successful] if lc= n+1, then set lc=0.
5.    stop.


Related Articles :



Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Shaadi.com Matrimony - Register for FREE