Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
soubhardwaj authored Oct 14, 2020
1 parent 52f37f9 commit 5bebb6c
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions BinarySearch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include<stdio.h>
int main()
{
int arr[50],n,search;
printf("Enter no. of element\n");
scanf("%d",&n);
printf("Enter the array element");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("Enter element those you want to search");
scanf("%d",&search);
int first = 0;
int last = n-1;
int middle =(first+last)/2;
while(first<=last)
{
if(arr[middle]<search)
{
first = middle + 1;
}
else if(arr[middle]==search)
{
printf("position:%d",middle+1);
break;
}
else{
last = middle - 1;
}
middle = (first+last)/2;
}
if(first>last){
printf("not found");
}
return 0;
}

0 comments on commit 5bebb6c

Please sign in to comment.