Skip to content

Create binarySearch.cpp #423

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: master
Choose a base branch
from
Open

Create binarySearch.cpp #423

wants to merge 1 commit into from

Conversation

Sejzz
Copy link

@Sejzz Sejzz commented Oct 22, 2023

I am creating a pull request for...

  • New algorithm
  • Update to an algorithm
  • Fix an error
  • Other - Describe below

@Manishmine
Copy link

//write a code for binary search in array
#include <stdio.h>
int main()
{
int low, high, middle, search, num[10]={1,2,3,4,5,6,7,8,9,10};

printf("Enter value to find : ");
scanf("%d", &search);

low = 0;
high = 10;
middle = (low+high)/2;

while(low<=high){
	if(num[middle] == search){
		printf("element %d was found at index %d", search, middle);
		break;
	}
	else if(num[middle]<search){
		low=middle+1;
	}
	else{
		high=middle-1;
	}
	middle= (low+high)/2;
}

if(low>high){
	printf("not found!!");
}

return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants