//binary search by self
#include <stdio.h>
int main()
{
int a[50],i,key,mid;
printf("\nEnter size of array (n) : ");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter integer value to search in array : ");
scanf( "%d", &key );
//Trying to sort the array in ascending order
for(i=0;i<n;i++){
l=0;
r=n-1;
mid=(l+r)/2;
if (mid==key){
printf("Element found at index %d",mid);
}else if(key>mid){
l=mid;
}else{
r=n-1;
}
}
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: