c icon indicating copy to clipboard operation
c copied to clipboard

Create binarySearch.cpp

Open Sejzz opened this issue 2 years ago • 1 comments

I am creating a pull request for...

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

Sejzz avatar Oct 22 '23 13:10 Sejzz

//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; }

Manishmine avatar Nov 24 '24 14:11 Manishmine