Implement a function of sequential search and count the steps executed by function on various inputs for best case and worst case. Also write complexity in each case and draw a comparative chart.


#include<stdio.h>
#include<conio.h>
#define size 10
unsigned long count=0;
void linear_search(int a[],int no)
{
      int i;
      count++;
      for(i=0;i<size;i++)
      {
                  count++;
                  count++;
                  if(a[i]==no)
                  {
                              count++;
                              return;
                  }
      }
      count++;
      count++;
      return;
}
void main()
{
      int a[size],i,no,index;
      clrscr();
      for(i=0;i<size;i++)
      {
                  a[i]=i+1;
      }
      printf("Enter which element you want to find : ");
      scanf("%d",&no);
      linear_search(a,no);
      printf("Count=%lu",count);
      getch();
}

Comments