我想进行二进制搜索,但结果有误
I wanted to do a Binary search but the result is faulty
我想对列表进行二分查找,但结果显示 'false',即使我检查了列表中的数字。
def clist(a):
l = [2,6,5,9,7,1,4,8,3]
newl = sorted(l)
check = int(1+len(newl)/2)
if newl[check] == a:
return True
if check > a:
for x in newl[:check]:
if x == a:
return True
return False
if check < a:
for x in newl[check::]:
if x == a:
return True
return False
print(clist(7))
请仔细阅读:
def clist(a):
l = [2,6,5,9,7,1,4,8,3]
newl = sorted(l)
check = int(1+len(newl)/2)
if newl[check] == a:
return True
if newl[check] > a: #fixed the bug here
for x in newl[:check]:
if x == a:
return True
if newl[check] < a: #fixed the bug here
for x in newl[check:]:
if x == a:
return True
return False #Return false should be universal. When the entire search fails it should be called.
print(clist(7))
你的函数不是二分查找,你是在检查了中间元素之后逐个元素地检查排序列表。
def binary_search(arr, i):
n = len(arr)
arr = sorted(arr)
left = 0
right = n - 1
# Define the condition when the loop should be broken
while (left <= right):
mid = left + (right-left) // 2
if arr[mid] == i:
return True
elif arr[mid] < i:
left = mid + 1
else:
right = mid - 1
return False
l = [2,6,5,9,7,1,4,8,3]
i = 7
binary_search(l, i)
您可以这样编写脚本:
- 取列表中间的元素
- return 如果这就是你需要的
- 如果你的
needle
比中间的gt,那么在列表的剩余右侧调用bsearch
- 另外用左边叫
bsearch
def bsearch(needle, haystack):
l = len(haystack)
half = int(l / 2)
element = haystack[half];
if element == needle:
return element
if needle <= element:
return bsearch(needle, haystack[0:half])
if needle > element:
return bsearch(needle, haystack[half:l])
print(bsearch(7, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
在二进制搜索中:
- 列表必须排序
- 如@tripleee所述,您必须递归地将列表分成两半
我想对列表进行二分查找,但结果显示 'false',即使我检查了列表中的数字。
def clist(a):
l = [2,6,5,9,7,1,4,8,3]
newl = sorted(l)
check = int(1+len(newl)/2)
if newl[check] == a:
return True
if check > a:
for x in newl[:check]:
if x == a:
return True
return False
if check < a:
for x in newl[check::]:
if x == a:
return True
return False
print(clist(7))
请仔细阅读:
def clist(a):
l = [2,6,5,9,7,1,4,8,3]
newl = sorted(l)
check = int(1+len(newl)/2)
if newl[check] == a:
return True
if newl[check] > a: #fixed the bug here
for x in newl[:check]:
if x == a:
return True
if newl[check] < a: #fixed the bug here
for x in newl[check:]:
if x == a:
return True
return False #Return false should be universal. When the entire search fails it should be called.
print(clist(7))
你的函数不是二分查找,你是在检查了中间元素之后逐个元素地检查排序列表。
def binary_search(arr, i):
n = len(arr)
arr = sorted(arr)
left = 0
right = n - 1
# Define the condition when the loop should be broken
while (left <= right):
mid = left + (right-left) // 2
if arr[mid] == i:
return True
elif arr[mid] < i:
left = mid + 1
else:
right = mid - 1
return False
l = [2,6,5,9,7,1,4,8,3]
i = 7
binary_search(l, i)
您可以这样编写脚本:
- 取列表中间的元素
- return 如果这就是你需要的
- 如果你的
needle
比中间的gt,那么在列表的剩余右侧调用bsearch
- 另外用左边叫
bsearch
def bsearch(needle, haystack):
l = len(haystack)
half = int(l / 2)
element = haystack[half];
if element == needle:
return element
if needle <= element:
return bsearch(needle, haystack[0:half])
if needle > element:
return bsearch(needle, haystack[half:l])
print(bsearch(7, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
在二进制搜索中:
- 列表必须排序
- 如@tripleee所述,您必须递归地将列表分成两半