鱼C论坛

 找回密码
 立即注册
查看: 3134|回复: 47

[作品展示] Python中的二分查找

[复制链接]
发表于 2023-4-29 10:40:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 陶远航 于 2023-4-29 10:47 编辑

Python中的二分查找(纯Python实现)
注意输入时要用英文的逗号
记得评分
源码:
  1. from __future__ import annotations
  2. import bisect

  3. def bisect_left(
  4.     sorted_collection: list[int], item: int, lo: int = 0, hi: int = -1
  5. ) -> int:
  6.     if hi < 0:
  7.         hi = len(sorted_collection)

  8.     while lo < hi:
  9.         mid = lo + (hi - lo) // 2
  10.         if sorted_collection[mid] < item:
  11.             lo = mid + 1
  12.         else:
  13.             hi = mid

  14.     return lo


  15. def bisect_right(
  16.     sorted_collection: list[int], item: int, lo: int = 0, hi: int = -1
  17. ) -> int:
  18.     if hi < 0:
  19.         hi = len(sorted_collection)

  20.     while lo < hi:
  21.         mid = lo + (hi - lo) // 2
  22.         if sorted_collection[mid] <= item:
  23.             lo = mid + 1
  24.         else:
  25.             hi = mid

  26.     return lo


  27. def insort_left(
  28.     sorted_collection: list[int], item: int, lo: int = 0, hi: int = -1
  29. ) -> None:
  30.     sorted_collection.insert(bisect_left(sorted_collection, item, lo, hi), item)


  31. def insort_right(
  32.     sorted_collection: list[int], item: int, lo: int = 0, hi: int = -1
  33. ) -> None:
  34.     sorted_collection.insert(bisect_right(sorted_collection, item, lo, hi), item)


  35. def binary_search(sorted_collection: list[int], item: int) -> int | None:
  36.     left = 0
  37.     right = len(sorted_collection) - 1

  38.     while left <= right:
  39.         midpoint = left + (right - left) // 2
  40.         current_item = sorted_collection[midpoint]
  41.         if current_item == item:
  42.             return midpoint
  43.         elif item < current_item:
  44.             right = midpoint - 1
  45.         else:
  46.             left = midpoint + 1
  47.     return None


  48. def binary_search_std_lib(sorted_collection: list[int], item: int) -> int | None:
  49.     index = bisect.bisect_left(sorted_collection, item)
  50.     if index != len(sorted_collection) and sorted_collection[index] == item:
  51.         return index
  52.     return None


  53. def binary_search_by_recursion(
  54.     sorted_collection: list[int], item: int, left: int, right: int
  55. ) -> int | None:
  56.     if right < left:
  57.         return None

  58.     midpoint = left + (right - left) // 2

  59.     if sorted_collection[midpoint] == item:
  60.         return midpoint
  61.     elif sorted_collection[midpoint] > item:
  62.         return binary_search_by_recursion(sorted_collection, item, left, midpoint - 1)
  63.     else:
  64.         return binary_search_by_recursion(sorted_collection, item, midpoint + 1, right)


  65. if __name__ == "__main__":
  66.     user_input = input("请用逗号分隔输入数字(从小到大):").strip()
  67.     #一定要用英文的逗号
  68.     collection = sorted(int(item) for item in user_input.split(","))
  69.     target = int(input("请输入要在列表中查找的单个数字:"))
  70.     result = binary_search(collection, target)
  71.     if result is None:
  72.         print(f"{target} 未在 {collection} 中找到.")
  73.     else:
  74.         print(f"{target} 被找到在 {result} 位置在 {collection} 中.")
复制代码

你没评分吧
赶紧的(还有44积分就到中级鱼油了,给个荣誉也行啊,或者评个鱼币回点血)
各种排序算法:https://fishc.com.cn/thread-227638-1-1.html

评分

参与人数 7荣誉 +13 鱼币 +18 贡献 +9 收起 理由
落花盈满绣! + 1 感谢楼主无私奉献!
不会起名字的我 + 5 鱼C有你更精彩^_^
汐澜sama + 1 鱼C有你更精彩^_^
元豪 + 3 + 3 鱼C有你更精彩^_^
sfqxx + 4 + 3
歌者文明清理员 + 4 + 3 鱼C有你更精彩^_^
liuhongrun2022 + 5 + 5 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-4-29 10:44:53 | 显示全部楼层
感谢评分
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-29 10:45:57 | 显示全部楼层

回帖奖励 +5 鱼币

玉璧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-4-29 11:21:42 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-29 11:37:37 | 显示全部楼层

回帖奖励 +5 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-29 11:49:43 | 显示全部楼层

回帖奖励 +5 鱼币

重复发帖(
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-4-29 12:51:58 | 显示全部楼层

已经删了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-29 13:29:01 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-4-29 13:37:56 | 显示全部楼层
@不二如是 求支持
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-29 14:32:54 | 显示全部楼层
顶~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-29 14:33:36 | 显示全部楼层
币币~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-29 14:35:05 | 显示全部楼层
币币币币币币币币币币......
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-4-29 14:53:22 | 显示全部楼层
元豪 发表于 2023-4-29 14:35
币币币币币币币币币币......

中奖绝缘体...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-29 15:03:28 | 显示全部楼层

55555
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-29 15:04:04 | 显示全部楼层

回帖奖励 +5 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-29 15:18:48 | 显示全部楼层
陶远航 发表于 2023-4-29 13:37
@不二如是 求支持

加一些解释会更好
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-29 17:04:23 | 显示全部楼层
渔币
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-29 17:05:07 | 显示全部楼层

回帖奖励 +5 鱼币

再来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-29 20:59:51 | 显示全部楼层

回帖奖励 +5 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-5-1 13:32:32 | 显示全部楼层
6666
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-13 21:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表