• Linear Search

    Linear Search

    Introduction:

    Linear search technique is also known as sequential search technique. The linear search is a method of searching an element in a list with sequential manner. In this technique, the array/list is searched for the given element from the beginning to end of array/list. The searching as continued up to element has found or array/list has finished.

    Algorithm of Linear (Sequential) Search:

    Step 1:  Set flag to “element not found”Step 2:  Select the first element in the list

    Step 3:  If the element in the list is equal to the desired element

    Set flag to “element found”

    Display the message “element found in the list”

    Go to step 6

    Step 4:  If it is not the end of list,

    Take the next element in the list

    Go to step 3

    Step 5:  If the flag is “element not found”

    Display the message “element not found”

    Step 6:  End of the Algorithm

    Advantages  of Linear (Sequential) Search:

    1. It is simple and conventional method of searching data. The linear or sequential name implies that the items are stored in a systematic manner.
    2. The elements in the list can be in any order. i.e. The linear search can be applied on sorted or unsorted linear data structure.

    Disadvantage of Linear (Sequential) Search:

    1. This method is insufficient when large number of elements is present in list.
    2. It consumes more time and reduces the retrieval rate of the system.

    Linear Search Recursive Program Code

    Example – Linear (Sequential) Search on Array/List using C Language

    #include<stdio.h>int linear(int [ ],int,int);void main( ){int a[20], pos = -1, n, k, i;clrscr( );printf(“\nEnter the n value:”);scanf(“%d”,&n);

    printf(“\nEnter elements for an array:”);

    for(i=0; i<n ;i++)

    scanf(“%d”,&a[i]);

    printf(“\nEnter the element to be searched:”);

    scanf(“%d”,&k);

    pos=linear(a,n,k);

    if(pos != -1)

    printf(“\n Search successful element found at position %d”,pos);

    else

    printf(“\n Search unsuccessful, element not found”);

    getch( );

    }

    int linear(int a[ ],int n,int k)

    {

    int i;

    for(i=0;i<n;i++)

    {

    if(a[i]==k)

    return(i);

    }

    return -1;

    }

    Appreciate my work :Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrDigg thisShare on YummlyShare on VKFlattr the authorBuffer this page

Leave a Reply

Your email address will not be published. Required fields are marked *