Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- void Binary_Search(int arr[] , int find , int n){
- int low = 0 , high = n-1 , mid = (high + low) / 2, check=0;
- while(low<=high){
- if(arr[mid] == find){
- check = 1;
- break;
- }
- else if(find > arr[mid]){
- low = mid + 1;
- }
- else{
- high = mid - 1;
- }
- mid = (high + low) /2;
- }
- if(check){
- printf("Element %d found at %d position",find,mid+1);
- }
- else printf("Not found");
- }
- int main()
- {
- int n,arr[100],find;
- printf("Enter the number of element(upto 100) and elements with spaces\n");
- scanf("%d",&n);
- for(int i=0 ; i<n ; i++){
- scanf("%d",&arr[i]);
- }
- printf("Enter the number you want to find ");
- scanf("%d",&find);
- Binary_Search(arr,find,n);
- }
- /*
- Output->
- Enter the number of element(upto 100) and elements with spaces
- 3
- 1 2 3
- Enter the number you want to find 3
- Element 3 found at 3 position
- */
Add Comment
Please, Sign In to add comment