Data-Structures-and-Algorithms-in-cpp icon indicating copy to clipboard operation
Data-Structures-and-Algorithms-in-cpp copied to clipboard

Bug at binary search

Open mohanadtalat91 opened this issue 3 years ago • 4 comments

Overflow will be happen at this line because if end=2147483647 & start=214 then the sum of start+end will be overflow. So try to use the following formula to prevent overflow - - > mid=start+((end-start)/2)

https://github.com/amritansh22/Data-Structures-and-Algorithms-in-cpp/blob/1d8ccd7b89ef093f173e80b9a3932104511062e5/SearchingAlgorithms/binary_search.cpp#L14

mohanadtalat91 avatar Mar 19 '22 12:03 mohanadtalat91

Thanks for opening your first issue here!

welcome[bot] avatar Mar 19 '22 12:03 welcome[bot]

Instead of using mid = (start+end)/2 , use mid = start+((end-start)/2).

Andyy-18 avatar Aug 21 '22 07:08 Andyy-18