Searching

Searching is a process of finding the correct location of an element from a list or array of elements. In an array, elements are stored in consecutive memory locations with the remaining element until the exact match is found. If the element is found, the search process is said to be successful or else the search process is terminated unsuccessfully.

Sequential Searching

Linear searching is nothing but searching an element in a linear way. This can be an array or in a linked list. We have a need to start the search from beginning and scan the element one by one until the end of the array or linked list. If the search is successful then it will return the location of the element, otherwise, it will return the failure notification.

linear search implementation

#include<stdio.h>

#define size 10
int main()
{
  int arr[size];
  int i,data,position=0;
  printf("\nENTER %d VALUES",size);
  for(i=0;i<size;i++)
   scanf("%d",&arr[i]);
  printf("\nENTER THE SEARCHING ELEMENT: ");
  scanf("%d",&data);
  
  for(i=0;i<size;i++)
  {
    if(arr[i]==data)
    { 
      position=i+1;
      break;
    }
  }
  if(position!=0)
   printf("\n%d is in %d position",data,position);
  else
   printf("\nDATA NOT FOUND");
 return 0;
}

Leave a Reply

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