Sunday 4 January 2015

Linear Search in C

Search - Searching is finding any item from a set of items which meets a specified condition.
For example - searching tallest student of a class from a provide list of students with their hight details.

Searching is very important aspect of most of the computer applications. Different types of data structures are created overtime to facilitate fast searches.

Examples are linear data structures like arrays or link lists and some more recent data structures like hash table or binary search trees.

Since we want to learn the algorithms for search hence it is in our own interest to apply those on data structures which are not designed for efficient data searching like binary search trees or hash tables.
Types of search algorithms which can be applied on linear data structures like array or linked list are linear search and binary search.

Linear Search - It is the most basic search algorithm which will search for the required element by traversing each element in the set sequentially.If the item which we are looking for is found search is stopped else proceeds to the next element until all the elements are traversed.

Linear Search in C -

#include <stdio.h>
void main()
{
     void linear_search(int *a, int b, int c);
    
     int i,array_size,array[20],element_to_search; 
    
     printf(" Enter the number of elements you want to enter in the array : \t ");
     scanf("%d",&array_size);
    
     for(i=0;i<array_size;i++)
     {
     printf("\n Enter the %d element : \t",i+1);
     scanf("%d",&array[i]);                        
     }
    
     printf("\n The set of elements in which we need to search the element is :  ");
     for(i=0;i<array_size;i++)
     printf("%d  ",array[i]);
    
     printf("\n Enter the element to be searched :  ");
     scanf("%d",&element_to_search);
    
     linear_search(&array,array_size,element_to_search); 
       
     getch();    
 }
// Below is the actual function which do the linear search. Note - This function returns all the occurrences of the element in the data set. With very small modifications it can be modified to stop execution when the required element is found for the first time. Just uncomment the code if condition and comment the existing code in there.

 void linear_search(int *array, int array_size, int element_to_search)
 {
      int i;
      int found = 0;
     
      for(i=0;i<array_size;i++)
      {
       if(element_to_search==array[i])
       {
       printf("\n Entered element's postion in the set of elements entered is :\t %d",i+1);
       found = 1;


      //  printf("\n Entered element's first occurrence in the set of elements entered is at :\t %d",i+1);
     //   found = 1;
     //   break;



       }
       }
       if(found==0)
       {
       printf("\n Entered element is not found in the set");
       }                       
       
  }
 
  Analysis of Linear Search -
 
  Worst case - When the element we want to search is not present in the given set. Linear search will compare element to be searched with all the
  elements in the array or list. Therefore the worst case complexity in this case will be O(n).
 
  Average case - Average can be considered when the element we want to search is present at the middle position of the array. Complexity in that
  case too will be O(n/2) ~ O(n). This can also be derived by simply considering average case as mean of worst case complexity and best case
  complexity.
 
  Best case - When the element we want to search for is present at the first location of the array. This will take O(1) time every time. This is a
  constant and do not depend on the length of array. 
 

No comments:

Post a Comment