0% found this document useful (0 votes)
8 views

#Include #Include

The document describes a C program that implements a binary search algorithm to search for a key element in a sorted array. The program takes user input of array size and elements, searches for a key, and outputs the position if found or not found.

Uploaded by

Tony
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

#Include #Include

The document describes a C program that implements a binary search algorithm to search for a key element in a sorted array. The program takes user input of array size and elements, searches for a key, and outputs the position if found or not found.

Uploaded by

Tony
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

BINARY SEARCH PROGRAM

#include <stdio.h>
#include <stdlib.h>

int binsrc(int x[], int low , int high , int key)


{
int mid;
while(low<=high)
{
mid=(low+high)/2;
if(x[mid]==key)
return mid;
if(x[mid]<key)
low=mid+1;
else
high=mid-1;
}
return -1;
BINARY SEARCH PROGRAM

int main()
{
int a[20],key,i,n,succ;
printf("Enter the n value\t");
scanf("%d",&n);
if(n>0)
{

printf("Enter the element in ascending


order\n");
for(i=0;i<=n;i++)
scanf("%d",&a[i]);
printf("Enter key element to be
searched\n");
scanf("%d",&key);
BINARY SEARCH PROGRAM

succ=binsrc(a,0,n-1,key);
if(succ>=0)
printf("Element found in position=
%d\n",succ+1);
else
printf("element not found\n");
}
else
printf("No element should be >0\n");
return 0;
}

You might also like