0

Binary search

https://code.sololearn.com/cn1tMXFLR9PX/#java In this program output for searching is always showing an item not found even if it is present over there.

9th Jul 2022, 10:26 PM
Jai Anand
Jai Anand - avatar
2 Answers
+ 1
I would place the if( c > 0 ) etc part outside the while loop.
9th Jul 2022, 10:35 PM
Dennis
Dennis - avatar
+ 1
Your array may not be sorted and thus not find the values searched for: example: 5 elements 1, 4, 5, 2, 3 search for 2 first search is done on indice 2 which equals 5 the search value is 2 so 2, 3 are cut out of the remainder of the search.
10th Jul 2022, 12:31 AM
William Owens
William Owens - avatar