Linear Search using ‘c

Direct hunt, otherwise called consecutive pursuit, is a straightforward inquiry calculation used to track down a particular component inside a rundown or exhibit of components. It works by consecutively checking every component of the rundown until a match is found or the whole rundown has been navigated.


Here is a bit by bit portrayal of the straight inquiry calculation:


1. Begin toward the start of the rundown.


2. Look at the objective component (the component you're looking for) with the ongoing component in the rundown.


3. Assuming that the ebb and flow component matches the objective component, the hunt is effective, and the position or list of the component is returned.


4. In the event that the ongoing component doesn't match the objective component, move to the following component in the rundown.


5. Rehash stages 2 to 4 until a match is found or the finish of the rundown is reached.


6. Assuming the whole rundown has been navigated without finding a match, the hunt is ineffective, and an assigned worth, (for example, - 1 or invalid) is gotten back to demonstrate that the objective component was not found.


Direct hunt is a clear and simple to-execute calculation, however it may not be effective for enormous records or clusters. Its time intricacy is O(n), where n is the quantity of components in the rundown. This implies that the most dire outcome imaginable happens when the objective component is either the last component or not present in the rundown, bringing about the calculation repeating through every one of the components.

 program

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

     int n;

     int item,i;

    printf("enter the no of items");

    scanf("%d",&n);

    int a[n];

    printf("Enter the items in array");

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

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

 printf("enter the item to searched ");

    scanf("%d",&item);

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

    {

        if(item==a[i])

        {

          printf("item %d is found at location %d",a[i],i);

            break;

        }

    }

    if(item!=a[i])

    {

       printf("item is not found");

    }

    return 0;

}

 Output