当前位置: 首页 > 科技观察

每日一技能:二分查找在分布式系统中也有用?_0

时间:2023-03-20 00:34:03 科技观察

相信大家都知道二分查找。在有序列表中,使用二分查找可以快速判断目标是否在列表中,时间复杂度为O(logN)。二分查找的代码很简单,使用递归只需要几行代码:defbinary_search(sorted_list,target):"""sorted_listisamonotonicallyincreasinglist"""ifnotsorted_list:returnFalsemid=len(sorted_list)//2iftarget>sorted_list[mid]:returnbinary_search(sorted_list[mid+1:],target)eliftarget