Showing posts with label Search Programs. Show all posts
Showing posts with label Search Programs. Show all posts

Friday, 5 December 2014

C Program for Linear Search Algorithm

Linear Search:

                    The linear search method is mainly applicable for searching elements with an unordered list.  In the linear Search method each element of the list is compared with the key in a sequential order. Linear Search is also called as Sequential Search.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,val,a[20],j=0;
clrscr();
printf("\nEnter the Number of Elements:");
scanf("%d",&n);
printf("\nEnter %d of elements:",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the Search element:");
scanf("%d",&val);
for(i=0;i<n;i++)
{
if(a[i]==val)
{
printf("\nElement Found");
}
else
{
j=j+1;
}
}
if(i==j)
{
printf("\nElement not found");
}
getch();

}

Output:

Enter the Number of Elements:4

Enter 4 of elements:6
2
1
9

Enter the Search element:10

Element not found



C Program for Binary Search Algorithm

Program:

#include<stdio.h>
#include<conio.h>
void main( )
{
    int a[20],val,i,n,low,high,mid;
    clrscr( );
    printf("\nEnter the Number of Elements:");
    scanf("%d",&n);
    printf("\nEnter the %d of elements:",n);
    for(i=0;i<n;i++)
    {
    scanf("%d",&a[i]);
    }
    printf("\nThe Elements in the array are :");
    for(i=0;i<n;i++)
    {
    printf("\n%d",a[i]);
    }
    low=0;
    high=n-1;
    printf("\nEnter the Search Element:");
    scanf("%d",&val);
    while(low<=high)
   {
  mid=(low+high)/2;
  if(a[mid]==val)
  {
   printf("\nElement found");
   break;
  }
  else if(a[mid]>val)
  {
   low=mid-1;
  }
  else if(a[mid]<val)
  {
   low=mid+1;
  }
  else
  {
   printf("\nElement not found");
  }
    }
    if(low>high)
    {
    printf("\nElement not found");
    }
}


Output:

Enter the Number of Elements:5

Enter the 5 of elements:1
2
3
4
5

The Elements in the array are :
1
2
3
4
5

Enter the Search Element:5

Element found




C Program for Binary Search using Functions

Binary Search:

Binary Search Technique is implemented on sorted list of elements. It is faster than linear search, hence this method is efficient when the number of elements are large.

Program:

#include<stdio.h>
#include<conio.h>
int binarysearch(in[],int,int,int);
void main( )
{
             int a[20],val,i,n,low,high,bsf;
             clrscr( );
             printf("\nEnter the Number of Elements:");
             scanf("%d",&n);
             printf("\nEnter the %d of elements:",n);
             for(i=0;i<n;i++)
             {
                     scanf("%d",&a[i]);
             }
             printf("\nThe Elements in the array are :");
             for(i=0;i<n;i++)
             {
                     printf("\n%d",a[i]);
             }
             low=0;
             high=n-1;
             printf("\nEnter the Search Element:");
             scanf("%d",&val);
             bsf=binarysearch(a,val,low,high);
             if(bsf == -1)
             {
                        printf("\nElement is found");
             }
             else
             {
                        printf("\nElement not found");
             }
             getch( );
}
int binarysearch(int a[ ], int val, int low, int high)
{
            int mid;
            while(low<=high)
            {
                   mid=(low+high)/2;
                   if(a[mid]==val)
                   {
                            return mid;
                   }
                   else if(a[mid]>val)
                   {
                            low=mid-1;
                   }
                   else if(a[mid]<val)
                   {
                            low=mid+1;
                   }
                   else
                   {
                            return -1;
                   }
             }
             return -1;
}

Output:


Enter the Number of Elements:5

Enter the 5 of elements:
1
2
3
4
5

The Elements in the array are :
1
2
3
4
5

Enter the Search Element:10

Element not found