diff --git a/C/Binary_Search.c b/C/Binary_Search.c new file mode 100644 index 0000000..4727c92 --- /dev/null +++ b/C/Binary_Search.c @@ -0,0 +1,45 @@ +//Program to perform binary search on an array +#include +#include + +// A comparator function used by qsort +int compare(const void * a, const void * b) +{ + return ( *(int*)a - *(int*)b ); +} + +int main() { + int n,s,i,low,high,mid,A[1000]; + printf("Enter the number of elements"); + scanf("%d",&n); + + printf("Enter the elements of array"); + for(i=0;is) // The element we are searching lies on the left side of middle element + high=mid-1; + else + low=mid+1; // The element we are searching lies on the right side of middle element + } + + if(low<=high) + printf("Element is present at index %d",mid); + else + printf("Element not present in the array"); + + return 0; +}