0

for binary search

#include<stdio.h> int binarysearch(int arr[],int element,int first,int last) { while(first<=last) { int mid=first+(last-first)/2; if(element==arr[mid]) { return mid; } else if(element<arr[mid]) { last=mid-1; } else { first=mid+1; } } return -1; } int main() { int arr[]={2,4,6,8,10,12}; int first=0;int element; int last=sizeof(arr)/sizeof(arr[0]); int result=binarysearch(arr,element,first,last); printf("Enter any number:"); scanf("%d",&element); if(result==-1) { printf("Found at %d index"); } else { printf("Not Found"); } return 0; }

14th Oct 2022, 3:12 PM
Priyanshu Kumar
6 Answers
0
Please LINK YOUR CODE, instead of pasting it into the description. This way it is easier to read your code and people can test it. Go to Code section, click +, select the programming language, insert your code, save. Come back to this thread, click +, Insert Code, sort for My Code Bits, select your code. Please clarify what your question is.
14th Oct 2022, 3:42 PM
Lisa
Lisa - avatar
0
ok
14th Oct 2022, 3:52 PM
Priyanshu Kumar
0
Ho gaya
14th Oct 2022, 4:00 PM
Priyanshu Kumar
0
answer please!
14th Oct 2022, 4:00 PM
Priyanshu Kumar
14th Oct 2022, 4:06 PM
Lisa
Lisa - avatar
0
Please clarify what your question is. Is see some issues in your code: 1. Put line 31 after line 33. You need to assign a value to element before passing it to your function 2. In the printf in line 36, result is missing. 3. In line 34, I think you confused Found vs. Not found.
14th Oct 2022, 4:09 PM
Lisa
Lisa - avatar