【发布时间】:2016-05-15 11:27:53
【问题描述】:
我正在尝试让二进制搜索在 Python 中工作。我有一个庞大的、排序的密码列表。计划是从用户那里获取密码输入并查看它是否在列表中。由于列表的大小,我决定实现二进制搜索。
这是我的代码:
Found = False
Password = user_input("Enter a password: ")
with io.open('final.txt', encoding='latin-1') as myfile:
data = myfile.readlines()
low = 0
high = (int(len(data))+1)
while (low < high) and not Found:
mid = int((low+high)/2)
if data[mid] == Password:
Found = True
break
elif Password < str(data[mid]):
high = mid - 1
elif Password > str(data[mid]):
low = mid + 1
我猜是因为字符串比较?有任何想法吗?二进制搜索永远不会返回 true,即使我明确搜索我知道在列表中的内容。
我使用此代码对密码列表进行排序。
import io
with io.open('result.txt', encoding='latin-1') as myfile:
data = myfile.readlines()
def partition(data, start, end):
pivot = data[end] # Partition around the last value
bottom = start-1 # Start outside the area to be partitioned
top = end # Ditto
done = 0
while not done: # Until all elements are partitioned...
while not done: # Until we find an out of place element...
bottom = bottom+1 # ... move the bottom up.
if bottom == top: # If we hit the top...
done = 1 # ... we are done.
break
if data[bottom] > pivot: # Is the bottom out of place?
data[top] = data[bottom] # Then put it at the top...
break # ... and start searching from the top.
while not done: # Until we find an out of place element...
top = top-1 # ... move the top down.
if top == bottom: # If we hit the bottom...
done = 1 # ... we are done.
break
if data[top] < pivot: # Is the top out of place?
data[bottom] = data[top] # Then put it at the bottom...
break # ...and start searching from the bottom.
data[top] = pivot # Put the pivot in its place.
return top # Return the split point
def quicksort(data, start, end):
if start < end: # If there are two or more elements...
split = partition(data, start, end) # ... partition the sublist...
quicksort(data, start, split-1)
quicksort(data, split+1, end)
quicksort(data, 0, (int(len(data))-1))
with io.open('final.txt', 'w', encoding='latin-1') as f:
for s in data:
f.write(s)
排序后的列表如下所示:空格,然后是符号,然后是数字,然后是大写字母(按字母顺序排序),然后是普通字母(按字母顺序排序)。
【问题讨论】:
-
有什么问题?
-
二进制搜索永远不会返回 true,即使我明确搜索我知道在列表中的内容。在任何搜索之后,打印高或低总是返回 992352。
-
除了你的算法问题,还有两个注意事项:1)执行时间是读取文件的 99%:所以线性搜索是这里最好的方法。 2)如果您将密码存储在内存中,则 set 比 list 更好:
passwords=set(data),Password in passwords在您的方法是 O( ln(n)) 时在 0(1) 中解决您的问题。
标签: python string search binary-search