binary_search.c
33 linesc
DOWNLOAD
1// Aim: Program to perform Binary Search on an array.
2#include<stdio.h>
3void main(){
4	int a[30],n,s,i,m,l,h,c=0;
5	printf("Enter the limit of the array :");
6	scanf("%d",&n);
7	printf("enter the elements of the array :");
8	for(i=0;i<n;i++){
9		scanf("%d",&a[i]);
10	}
11	printf("Enter the elemant to be searched :");
12	scanf("%d",&s);
13	l=0;
14	h=n-1;
15	for(i=0;i<n;i++){
16		m=(l+h)/2;
17		if(s==a[m]){
18			c++;}
19		if(s<a[m]){
20			h=m-1;}
21		if(s>a[m]){
22			l=m+1;}
23	}
24	if(c>0){
25		printf("The element %d is found",s);
26	}
27	if(c==0){
28		printf("The element %d is not found",s);
29	}
30	
31
32}
33