Skip to content

Added Binary Search in Basic C Folder #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Basic_programs/C++/binarySearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <stdio.h>

int binarysearch(int arr[], int high, int low, int tbf)
{

while(low <= high){

int mid = low+(high-low);

if(arr[mid]==tbf){return mid;}

else if(arr[mid]>tbf){
low = mid+1;
// mid = (low+high)/2; // tbf to be found integer
// printf("IN THE ELSE IF STATEMENT THIS IS THE VALUE OF LOW MID AND HIGH %d %d %d \n",low,mid,high);
}
else if(arr[mid] < tbf){
high = mid - 1;
// mid = (low+high)/2;
}
}
}

int main()
{
int arrsize, high, low, tbf;
printf("Enter the size of array you want to create\n");
scanf("%d", &arrsize);

high = arrsize - 1;
low = 0;
int arr[arrsize];

for (int i = 0; i < arrsize; i++)
{
printf("Enter number at index %d\n", i);
scanf("%d", &arr[i]);
}
printf("Enter the number to be found out\n");
scanf("%d", &tbf);

int result= binarysearch(arr, high, low, tbf);
printf("Found at %d",result);

return 0;
}