二进制搜索python 3.5
我试图用Python3.5编写二进制搜索,但它不起作用,我不知道为什么二进制搜索python 3.5,python,runtime-error,binary-search,python-3.5,Python,Runtime Error,Binary Search,Python 3.5,我试图用Python3.5编写二进制搜索,但它不起作用,我不知道为什么 def binarySearch(alist, value): first = 0 last = len(alist)-1 midpoint = (last//2) while binarySearch: if value == alist[midpoint]: return True and print ("found") else:
def binarySearch(alist, value):
first = 0
last = len(alist)-1
midpoint = (last//2)
while binarySearch:
if value == alist[midpoint]:
return True and print ("found")
else:
if value < midpoint:
last = midpoint-1
else:
if value > midpoint:
first = midpoint+1
binarySearch([1,2,3,4,5,6,7,8],3)
def二进制搜索(列表,值):
第一个=0
last=len(alist)-1
中点=(最后//2)
二进制搜索时:
如果值==列表[中点]:
返回True并打印(“找到”)
其他:
如果值<中点:
最后=中点-1
其他:
如果值>中点:
第一个=中点+1
二进制搜索([1,2,3,4,5,6,7,8],3)
若我将值设置为4,它将显示find,若我设置了其他任何内容,则不会发生任何事情,并且它会在运行时被卡住,什么也不做
谢谢你的帮助
返回True并打印(“找到”)
它将始终不返回任何值User1915011抢先回答了我的问题。根据他的回答和@wim的评论,我对您的
binarySearch
方法做了以下更改
found
变量中点添加了一个附加赋值
first二进制转换器来确保循环终止
num = int(input('please enter your number: '))
list = []
for i in (128, 64, 32, 16, 8, 4, 2, 1):
if num >= i:
list.append(1)
num = num-i
else:
list.append(0)
print(list)
以下是其工作原理的详细说明:
def binarySearch(array, i):
## Binary search is the algorithm which is used to search an element in a sorted array
## The time complexity of the binary search is O(log n)
## Which means that in an array of length(2^(n-1)) elements we need to look at only n elements
## That is why we say that binary search algorithm runs in logarithmic time, which is much faster than linear time
start = 0
last = len(array)-1
result = False
count = 0 ## to debug
print("\n******************************************************************\n")
while(start <= last and not result):
## Debugger Begin
mid = 0
print("Loop number: ", count)
print("Start element: ", array[start], " Position of Start Element: ", start)
print("Last element: ", array[last], " Position of Last Element: ", last)
## Debugger End
mid = (start + last)//2 ## '//' indicates the floor division(ignores the value after the period)
if(array[mid] == i):
print("***Mid***")
result = True;
else:
if(i < array[mid]):
print("***The value of the item:",i," we are searching for is LESS than the current middle element***")
last = mid - 1
else:
print("***The value of the item:",i," we are searching for is GREATER than the current middle element***")
start = mid + 1
## Debugger
count = count+1
print("Mid element: ", array[mid], " Position of Mid Element: ", mid, "\n")
## Debugger
print("******************************************************************")
if(result == True):
print("\nThe element:",i ,"is in the array")
else:
print("\nItem is not in the array")
return result
## Array you want to search
array = [9, 11, 12, 21, 23, 34, 45, 49, 65, 98]
## Item you want to search in the array
i = 21
print("Searching the element: ",i , "\nIn the Array: ", array)
print("Length of the array is: ", len(array))
## Binary Search
binarySearch(array, i)
def二进制搜索(数组,i):
##二进制搜索是用于在排序数组中搜索元素的算法
##二进制搜索的时间复杂度为O(logn)
##这意味着在长度(2^(n-1))元素的数组中,我们只需要查看n个元素
##这就是为什么我们说二进制搜索算法在对数时间内运行,这比线性时间快得多
开始=0
last=len(数组)-1
结果=错误
计数=0##以进行调试
打印(“\n******************************************************************************************************************************\n”)
虽然(开始时,您从不在中重新指定中点
)loop@wim啊,好吧,我会试试的,我知道这可能是为了上课,但一般来说,不要写你自己的二进制搜索。Python已经有了确切的目的。是的,我理解,但它会在我的考试中出现,他们希望我能写出来。非常感谢老兄appreciated@Jaco因为如果在循环中指定中点,则无需预先指定。请注意,它不会使用last=len(alist)-1识别最后一个元素,它会将其识别为最后一个元素之前的元素。因此,在给定的示例中,它会认为7是最后一个元素,而不是8?谢谢。binarySearch([1,2,3,4,5,6,7,8])
似乎有效?在Python中,索引从0开始,因此a[len(a)-1]
如果a=[1,2,3,4,5,6,7,8]则返回8
def binarySearch(array, i):
## Binary search is the algorithm which is used to search an element in a sorted array
## The time complexity of the binary search is O(log n)
## Which means that in an array of length(2^(n-1)) elements we need to look at only n elements
## That is why we say that binary search algorithm runs in logarithmic time, which is much faster than linear time
start = 0
last = len(array)-1
result = False
count = 0 ## to debug
print("\n******************************************************************\n")
while(start <= last and not result):
## Debugger Begin
mid = 0
print("Loop number: ", count)
print("Start element: ", array[start], " Position of Start Element: ", start)
print("Last element: ", array[last], " Position of Last Element: ", last)
## Debugger End
mid = (start + last)//2 ## '//' indicates the floor division(ignores the value after the period)
if(array[mid] == i):
print("***Mid***")
result = True;
else:
if(i < array[mid]):
print("***The value of the item:",i," we are searching for is LESS than the current middle element***")
last = mid - 1
else:
print("***The value of the item:",i," we are searching for is GREATER than the current middle element***")
start = mid + 1
## Debugger
count = count+1
print("Mid element: ", array[mid], " Position of Mid Element: ", mid, "\n")
## Debugger
print("******************************************************************")
if(result == True):
print("\nThe element:",i ,"is in the array")
else:
print("\nItem is not in the array")
return result
## Array you want to search
array = [9, 11, 12, 21, 23, 34, 45, 49, 65, 98]
## Item you want to search in the array
i = 21
print("Searching the element: ",i , "\nIn the Array: ", array)
print("Length of the array is: ", len(array))
## Binary Search
binarySearch(array, i)