+ 1

It doesn't work properly. Please solve it.

Input size 5 Elements are 11 12 25 26 28 Find elements 25 Its not find show blank why anyone help. https://code.sololearn.com/cGxnJ3lz6m7Q/?ref=app

12th Jan 2022, 6:31 AM
PARMAR AAYUSH
PARMAR AAYUSH - avatar
2 Respuestas
+ 5
You should've made a separate binary search function if you want to return item index. But here you do the search inside main() so issuing a `return` will terminate the program, no matter what value you return. I would first add a condition to check where <ele> is either less than a[0] or greater than a[n - 1]. In which case the search should not be performed at all because <ele> obviously is not in array. You can add this conditional after the array is sorted, but before search begins. Comment line 41, 47, 48, 52, 53. return 0; at line 56, you're in main().
12th Jan 2022, 8:08 AM
Ipang
+ 3
It is because you are returning before printing. Change it like below. if(ele == a[mid]) { printf("\nvalue is ind at position %d \n",mid); return mid; break; }
12th Jan 2022, 7:51 AM
Avinesh
Avinesh - avatar