Binary Search algorithm implementations

后端 未结 2 999
旧巷少年郎
旧巷少年郎 2020-12-10 09:35

I have come across multiple problems which use variations of binary search to reach the final answer. These problems include finding floor of square root of

2条回答
  •  感情败类
    2020-12-10 10:20

    It's not like there are worse and better options that you've mentioned. Usually it just depends on a implementation, e.g. if you pass high=arr.length then you'd rather write while(low < high) than while(low <= high).

    In fact, some of those differences may have a meaning. If we consider binary search algorithm to find your X number in an array A and there will be some (not only one) elements equal to X, you can both find index of the first one or the last one - it depends on a implementation.

提交回复
热议问题