查看: 1500|回复: 1
收起左侧

[资料分享] 查找数字python实现

Lihoon 2020-3-20 18:24:35 | 显示全部楼层 |阅读模式
邀请回答

马上注册,享受更多特权

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

x
1、直接查找法
  1. #-*- coding:utf-8 -*-
  2. #Created Date: 2020/03/18

  3. import random
  4. import timeit
  5. import sys

  6. def random_list(n):
  7.         """返回一个长度为n的整数列表"""
  8.         list = []
  9.         for num in range(n):
  10.                 list.append(random.randrange(1000))
  11.         return list


  12. def quick_sort(list):
  13.         """采用快速排序法对生成的随机数组进行排序"""
  14.         if len(list) <= 1:
  15.                 return list
  16.         left = []
  17.         right = []
  18.         for idx in list[1:]:
  19.                 if idx <= list[0]:
  20.                         left.append(idx)
  21.                 else:
  22.                         right.append(idx)
  23.                        
  24.         return quick_sort(left) + [list[0]] + quick_sort(right)

  25. def sequential_search(list, key):
  26.         # print("list = {}".format(list))
  27.         # print("Find the number: %d" % key)
  28.         for i in range(len(list)):
  29.                 if list[i] == key:
  30.                         return i
  31.         return -1

  32. # 输入自定义数组长度
  33. len_list = input("\nPlease input the length of list: ")

  34. # 调用random_list()函数生成随机数组
  35. source_list = random_list(int(len_list))
  36. print("source_list:{}".format(source_list))

  37. # 调用quick_sort()函数排序数组
  38. sort_list = quick_sort(source_list)
  39. print("sorted list: {}".format(sort_list))

  40. # 手动输入关键词,调用sequential_search()函数搜索
  41. key = int(input("key: "))
  42. result = sequential_search(sort_list, key)

  43. if result >= 0:
  44.         print("\n{} is in the list, and the index is: {}".format(key, result))
  45. else:
  46.         print("\n{} is not in the list.".format(key))
复制代码


2、二分查找法
  1. #-*- coding:utf-8 -*-
  2. #Created Date: 2020/03/18

  3. import random
  4. import timeit
  5. import sys

  6. def random_list(n):
  7.         """返回一个长度为n的整数列表"""
  8.         list = []
  9.         for num in range(n):
  10.                 list.append(random.randrange(1000))
  11.         return list


  12. def quick_sort(list):
  13.         """采用快速排序法对生成的随机数组进行排序"""
  14.         if len(list) <= 1:
  15.                 return list
  16.         left = []
  17.         right = []
  18.         for idx in list[1:]:
  19.                 if idx <= list[0]:
  20.                         left.append(idx)
  21.                 else:
  22.                         right.append(idx)
  23.                        
  24.         return quick_sort(left) + [list[0]] + quick_sort(right)

  25. def binary_search(list, key):
  26.         list_len = len(list)
  27.         left = 0
  28.         right = list_len - 1
  29.        
  30.         while right - left > 1:
  31.                 mid = (left + right) // 2
  32.                 if key < list[mid]:
  33.                         right = mid
  34.                         print("finding...")
  35.                 elif key > list[mid]:
  36.                         left = mid
  37.                         print("finding...")
  38.                 else:
  39.                         print("finded")
  40.                         return mid

  41.         if key == list[left]:
  42.                 print("finded")
  43.                 return left
  44.         elif key == list[right]:
  45.                 print("finded")
  46.                 return right
  47.         else:
  48.                 print("not find")
  49.                 return -1

  50. # 输入自定义数组长度
  51. len_list = input("\nPlease input the length of list: ")

  52. # 调用random_list()函数生成随机数组
  53. source_list = random_list(int(len_list))
  54. print("source_list:{}".format(source_list))

  55. # 调用quick_sort()函数排序数组
  56. sort_list = quick_sort(source_list)
  57. print("sorted list: {}".format(sort_list))

  58. # 手动输入关键词,调用binary_search()函数搜索
  59. key = int(input("key: "))
  60. result = binary_search(sort_list, key)

  61. if result >= 0:
  62.         print("\n{} is in the list, and the index is: {}".format(key, result))
  63. else:
  64.         print("\n{} is not in the list.".format(key))
复制代码


3、斐波那契查找法
  1. #-*- coding:utf-8 -*-
  2. #Created Date: 2020/03/19

  3. import random
  4. import sys

  5. def random_list(num):
  6.         """生成随机数组"""
  7.         list = []
  8.         for i in range(num):
  9.                 list.append(random.randrange(999))
  10.         return list
  11.        
  12. def quick_sort(list):
  13.         """排序"""
  14.         if len(list) <= 1:
  15.                 return list
  16.        
  17.         left = []
  18.         right = []
  19.         for i in list[1:]:
  20.                 if i <= list[0]:
  21.                         left.append(i)
  22.                 else:
  23.                         right.append(i)
  24.         return quick_sort(left) + [list[0]] + quick_sort(right)
  25.        
  26. def fibonacci(n):
  27.         """定义斐波那契数列,并输出数列的最后一个值"""
  28.         fib_list = [1, 1]
  29.         while n > 1:
  30.                 fib_list.append(fib_list[-1] + fib_list[-2])
  31.                 n -= 1
  32.         return fib_list[-1]
  33.        
  34. def fibonacci_search(list, key):
  35.         """斐波那契数列搜索"""
  36.         len_list = len(list)
  37.         left = 0
  38.         right = len_list - 1
  39.         range_list = len_list - 1
  40.        
  41.         k = 1
  42.         while fibonacci(k) -1 < range_list:
  43.                 k += 1
  44.        
  45.         while range_list > 1:
  46.                 print("k = %d" % k)
  47.                 mid = left + fibonacci(k -1)
  48.                 if key < list[mid]:
  49.                         right = mid - 1
  50.                         k = 1
  51.                         range_list = right - left
  52.                 elif key == list[mid]:
  53.                         return mid
  54.                 else:
  55.                         left = mid + 1
  56.                         k = 1
  57.                         range_list = right - left
  58.         if key == list[left]:
  59.                 return left
  60.         elif key == list[right]:
  61.                 return right
  62.         else:
  63.                 return -1

  64. num = input("请输出随机数列的长度:")
  65. source_list = random_list(int(num))
  66. print("生成的随机数组为:{}".format(source_list))

  67. sorted_list = quick_sort(source_list)
  68. print("排序后的数组为:{}".format(sorted_list))

  69. key = int(input("\n请输入搜索的关键词:"))
  70. result = fibonacci_search(sorted_list, key)

  71. if result >= 0:
  72.         print("{}在这个数列中,它的下标是:{}.".format(key, result))
  73. else:
  74.         print("{}不在这个数列中.".format(key))
复制代码






上一篇:【排序】计数排序法python实现
下一篇:回文字符串验证

已有 0 人打赏作者

1 喜欢他/她就送朵鲜花吧,赠人玫瑰,手有余香! 鲜花榜单
回复 邀请回答送花

使用道具 举报

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

本版积分规则

有技术问题,就上汇川技术社区

INOVANCE汇川技术 公众号

扫码下载掌上汇川APP

全国服务热线:8:30-17:30

4000-300124

苏州地址:江苏省苏州市吴中区越溪友翔路16号

深圳地址:深圳市龙华新区观澜街道高新技术产业园汇川技术总部大厦

Copyright © 2003-2100 汇川技术 Powered by Discuz! X3.4 ( 苏ICP备12002088号 )
快速回复 返回列表 返回顶部