Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

Commit

Permalink
Add BinarySearch file (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
code2hel authored Oct 14, 2021
1 parent 831fbd8 commit 6307d13
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Programming/C/BinarySearch/Binary_Search.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include<stdio.h>

int BinarySearch(int arr[], int size, int element){
int low, mid, high;
low = 0;
high = arr[size-1];

// Start Searching : until low and high converges
while(low<=high)
{
mid = (low + high)/2;
if(arr[mid] == element)
{
return mid;
}
if(arr[mid] == element)
{
low = mid+1;
}
else
{
high = mid-1;
}
}

int main(){
int arr[] ={1,3,4,6,34,6,43,66,32};
int size = sizeof(arr)/sizeof(int);
int element = 6;
int SearchIndex = BinarySearch(arr, size, element);
printf("The element %d was found at index %d \n",element, SearchIndex);

return 0;
}

0 comments on commit 6307d13

Please sign in to comment.