How To Perform Linear Search In Data Structure

Searching in data structure means to find whether a particular value is present in an array or not. If the value is present in the array ,then searching is said to be successful and the searching process gives the location of that particular element in array. If the value is not present in the array the searching process display a message , and in this case searching is said to be unsuccessful. There are mostly two types of searching is used the first one is Linear Search and the second is Binary Search.

Types of Searching -:

  • Linear or Sequential Search ( For unordered array )
  • Binary Search ( For ordered array as ascending or descending )
  • Interpolation Search ( For ordered array )
  • Jump Search ( For ordered array )

In this section we will discuss about Linear Search and we will see the program of Linear Search.

Linear Search -:

Linear search , also called as sequential search , is a very simple method used for searching an array for particular value. it works by comparing the value to be searched with every element of array one by one in a sequence until a match is found. linear search is mostly used to search a value in an unordered array.

Note ( In linear search we find the position of given element to be searched )

How Linear Search Works -:

Let suppose we have an array like this ..

a[ ] ={ 2 , 5 , 7 , 9 , 4 , 8 }

if in this array we have to search 9 then program checks one by one every element until the value is matched..

first it matches with first element if matched it print the position otherwise counter will increase and check the next value when it found match it prints the position of that element.

Program of Linear search -:

#include<stdio.h>
#include<stdlib.h>
#define size 20
int main()
{
	int arr[size],num,i,n,found=0,pos=-1;
	printf("enter the number of element in array");
	scanf("%d",&n);
	printf("\n enter the element...");
	for(i=0; i<n; i++)
	{
		scanf("%d",&arr[i]);
	}
	printf("enter the number to search");
	scanf("%d",&num);
	for(i=0; i<n; i++)
	{
		if(arr[i]==num)
		{
			found=1;
			pos=i;
			printf("\n %d is found in array at position =%d",num,i);
			break;
		}
	}
	if(found==0)
	{
		printf("\n %d is not exist in this array",num);
		
	}
	return 0;
}

Conclusion -:

I hope this post helps to all of to understand the concept of searching and linear search ,in our next post we will discuss about binary search.

One thought on “How To Perform Linear Search In Data Structure

Add yours

Leave a Reply

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

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑