Translate

Wednesday 12 March 2014

Simple SEARCHING PROGRAMS in C Language

Note: All the programs given below have been successfully compiled and executed.To use it in your Program 
          1.Copy the program in Notepad
          2.Save it in tc/bin folder as Sort.C
          3.Execute and view your Output


Binary Search:

Searches for the element by Checking the Mid Element everytime the array breaks in two

Note: Requires the array to be Sorted before Search.
          Binary Search and Binary Search Tree are Two Different Things

#include<stdio.h>
#include<conio.h>

int binsSearch( inta[], int lower, int upper, int no)
{
   int mid;
   if( lower > upper) return( -1 );
   mid = ( lower + upper )/2;
   
   if( a[mid] == no ) return ( mid );
      else  if( a[mid] < no ) 
                return ( binSearch( a, mid+1, upper, no));
              else return  ( binSearch( a, lower, mid-1, no));
 }

void main( )
{  
    int no,pos;
    int a[]= { 5, 6, 7, 8, 9, 10}
    printf("Enter the element to search");
    scanf("%d ", &no);
    if( binSearch == -1)
        printf("Element not found");
    else printf(" Element found at position %d ", pos);
 }
   




No comments:

Post a Comment