• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Linear search in Data Structures

Linear search

The linear search in data structures  or the sequential searching is most simple searching method.

The key which is to be searched, is compared with each element of the list one by one in Linear Search

If a match exits, the search is terminated in Linear Search

If the end of the list is reached, it means that the search has failed and the key has no matching element in the list.

 

ex:

20

 

Program : 

Linear search

# include <stdio.h>

# include <conio.h>

int linear(int a[20]),int n, int key);

void main()

{

int a[20],n,i,key,p;

clrscr();

printf(“Enter the value \n”);

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

{

scanf(“%d”,&key);

}

printf (“Enter the search element \n”);

scanf(“%d”,&key);

p=liner(a,n,key);

if(p>=0)

{

printf(“The element found at %d position”,p+1);

}

else

printf(“The element is not found”);

getch();

}

int linear(int a[20], int n, int key)

{

int i=0;

while(i<n)

{

if(a[i]==key)

{

return j;

}

i++;

}

return i;

}

 

Time complexity:

In the worst case(i.e when there)  N  comparisons are required hence the worst case performance represented as o(n);

The best case in which the first comparison returns a match, it requires a single comparison and hence it is o(1)

The average case roughly requires N/2 comparisons to search the element. That means the average time, as in worst-case is proportional to N and hence is o(n)

 

 

Summary
Review Date
Reviewed Item
Linear search in Data Structures
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Linear search in Data Structures"

    Leave a Message

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

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.