Computer Science, asked by shinyazeez123, 10 months ago

write
program
to enter
10 values
into
and search
for
element entered by
the use
using binary search​

Answers

Answered by Anonymous
2

#include<iostream.h>

#include<conio.h>

void Bsearch(int a[],int);

void main( )

{

clrscr( );

int a[10],i,n;

cout<<"enter 10 element";

for(i=0;i<10;i++)

{

cin>>a[i];

}

cout<<"enter element to search";

cin>>n;

Bsearch(a[],n);

getch();

}

void Bsearch(int a[],int item)

{

int beg=0,end=9,mid,POS=0;

while(beg<=end)

{

mid=(beg+end)/2

if(item==a[mid])

{

POS=1;

cout<<"item found , position="<<mid+1;

break;

}

else if(item>a[mid])

{

beg=mid+1;

}

else

{

end=mid-1;

}

}

if(POS==0)

{

cout<<"item not found";

}

}

Happy coding !

Answered by srajfaroquee
0

Answer:

#include<stdio.h>

int BinarySearch(int A[],int lb,int ub,int n);

int main(){

   int ub,lb;

   int array[20];

   int x,y,n,i;

   printf("\n * Enter the no of elements in the array (max. 20): ");

   scanf("%d",&n);

   printf("\n * Enter the array elements (in Ascending order): \n");

   for(i=0;i<n;i++){

       scanf("%d",&array[i]);

   }

   printf("* Enter the number to be searched: ");

   scanf("%d",&x);

   y = BinarySearch(array,0,n-1,x);

   if(y<0){

       printf("\n ** Data not found.\n\n");

   }

   else{

       printf("\n ** Data found at position: %d \n\n ",y+1);

   }

       return 0;

}

int BinarySearch(int A[],int lb,int ub,int n){

   int mid;

   mid = (lb+ub)/2;

     if(lb<=ub){

       if(A[mid]==n){

           return mid;

       }

      else if(A[mid]<n){

           lb = mid + 1;

          BinarySearch(A,lb,ub,n);

       }

       else if(A[mid]>n){

           ub = mid - 1;

          BinarySearch(A,lb,ub,n);

       }

   }

else{

       return -1;

   }

}

**Please follow me and mark this ans as Branliest answer.Thank you!

Similar questions