Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
二进制搜索python 3.5_Python_Runtime Error_Binary Search_Python 3.5 - Fatal编程技术网

二进制搜索python 3.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:

我试图用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:
            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,若我设置了其他任何内容,则不会发生任何事情,并且它会在运行时被卡住,什么也不做

谢谢你的帮助

  • 当binarySearch时,您的循环条件是错误的
  • 您只需更改中点的值一次,而应该在每次循环迭代中更改它
  • 将值与索引(中点)进行比较,并应与 列表值(列表[中点])
  • 这是错误的:
    返回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)