Binary Search with ternary oparator

#include<stdio.h>
#include<conio.h>
int binSearch(int a[],int ,int ,int );
int main ()
{
	int n,i,a[10],low,high,ele,result;
   clrscr();
   printf("How many no you want to enter: ");
   scanf("%d",&n);
   printf("Enter the no:\n");
   for(i=0;i<n;i++)
   	scanf("%d",&a[i]);
   low=0;
   high=n;
   printf("Which no you want to search? ");
   scanf("%d",&ele);
   result=binSearch(a,low,high,ele);
   if (result==-1)
   	printf("Data not found");
   else
   	printf("Data found");
   getch();
}

int binSearch(int a[],int low,int high,int ele)
{
	int mid=(low+high)/2;
   if(low>high)
   	return -1;
   else
   {
   	return(ele==a[mid]?mid:ele>a[mid]?binSearch(a,mid+1,high,ele):binSearch(a,low,mid-1,ele));
   }
}

Related posts:

Leave a Reply

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