amritansh22 / Data-Structures-and-Algorithms-in-cpp

This repository is in development phase and will soon provide you with c++ code of various data structures and algorithms
MIT License
365 stars 341 forks source link

Bug at binary search #600

Open mohanadtalat91 opened 2 years ago

mohanadtalat91 commented 2 years ago

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

welcome[bot] commented 2 years ago

Thanks for opening your first issue here!

Andyy-18 commented 1 year ago

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