Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

二分查找算法实现 一文作者不小心,有误 #15

Closed
kiddx01 opened this issue Feb 13, 2015 · 0 comments
Closed

二分查找算法实现 一文作者不小心,有误 #15

kiddx01 opened this issue Feb 13, 2015 · 0 comments

Comments

@kiddx01
Copy link

kiddx01 commented Feb 13, 2015

原谅:

也可以不用递归方法,而采用循环,如下:

def binarySearch(lst, value,low,high):          #low,high是lst的查找范围
    if high < low:
        return -1
    mid = (low + high)/2
    if lst[mid] > value:
        return binarySearch(lst, value, low, mid-1)
    elif lst[mid] < value:
        return binarySearch(lst, value, mid+1, high)
    else:
        return mid

最后应该是
return lst[mid]

另外 当 high< low 时 返回-1 ,对此不解。 应该返回None

@kiddx01 kiddx01 closed this as completed Feb 13, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant