0% found this document useful (0 votes)
12 views1 page

Bin Search

The document contains code for a C program that performs a binary search on a sorted array. The program takes input for array size and elements, searches for a target element, and outputs whether the element was found or not.

Uploaded by

Brutal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

Bin Search

The document contains code for a C program that performs a binary search on a sorted array. The program takes input for array size and elements, searches for a target element, and outputs whether the element was found or not.

Uploaded by

Brutal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include<stdio.

h>
int main(){
int n;
printf("Enter size of array : ");
scanf("%d", &n);
int arr[n];
int i;
printf("Enter array elements in sorted order only\n");
for(i=0;i<n;i++)
scanf("%d", &arr[i]);
int search;
printf("Find element to search : ");
scanf("%d", &search);
int flag = 0;
int l =0,r=n-1;
while(l<=r){
int mid = (l+r)/2;
if(arr[mid]==search){
printf("Element found!!\n");
flag = 1;
break;
}
else if(arr[mid]<search)
l = mid + 1;
else
r = mid - 1;
}
if(flag == 0)
printf("Element not found\n");
return 0;
}

You might also like