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

四种版本的二分查找算法 #1

Open
xxleyi opened this issue Feb 21, 2019 · 3 comments
Open

四种版本的二分查找算法 #1

xxleyi opened this issue Feb 21, 2019 · 3 comments

Comments

@xxleyi
Copy link
Owner

xxleyi commented Feb 21, 2019

image

def bin_find_v1(v, lo, hi, e):
    if lo >= hi:
        return -1
    else:
        mi = (lo + hi) // 2
        if v[mi] == e:
            return mi
        if v[mi] > e:
            return bin_find_v1(v, lo, mi, e)
        else:
            return bin_find_v1(v, mi+1, hi, e)

v = [1, 2, 3, 4, 5, 6, 7]
bin_find_v1(v, 0, len(v), 3)

版本一:三个区间的二分查找,且头尾区间不均衡。
Python tutor 演示链接

@xxleyi
Copy link
Owner Author

xxleyi commented Feb 21, 2019

image

def bin_find_v3(v, lo, hi, e):
    if lo < hi - 1:
        mi = (lo + hi) // 2
        if e < v[mi]:
            hi = mi
        else:
            lo = mi
        return bin_find_v3(v, lo, hi, e)
    return (lo if v[lo] == e else -1)

v = [1, 2, 3, 4, 5, 6, 7]
bin_find_v3(v, 0, len(v), 4)

版本三:两个区间的二分查找,查找不到时返回 -1
Python tutor 演示链接

@xxleyi
Copy link
Owner Author

xxleyi commented Feb 21, 2019

image

def bin_find_v4(v, lo, hi, e):
    if lo < hi:
        mi = (lo + hi) // 2
        if e < v[mi]:
            hi = mi
        else:
            lo = mi + 1
        return bin_find_v4(v, lo, hi, e)
    return lo - 1
     
v = [1, 2, 3, 4, 5, 6, 7]

bin_find_v4(v, 0, len(v), 4)

版本四:两个区间的二分查找,返回不大于e的最后一个元素的 rank
Python tutor 演示链接

@xxleyi
Copy link
Owner Author

xxleyi commented Feb 21, 2019

image

def bin_find_v1(v, lo, hi, e):
    if lo >= hi:
        return -1
    else:
        mi = (lo + hi) // 2
        if v[mi] == e:
            return mi
        if v[mi] > e:
            return bin_find_v1(v, lo, mi, e)
        else:
            return bin_find_v1(v, mi+1, hi, e)

v = [1, 2, 3, 4, 5, 6, 7]
bin_find_v1(v, 0, len(v), 3)

版本一:三个区间的二分查找,且头尾区间不均衡。
Python tutor 演示链接


版本 2: 黄金分割二分查找

image

def bin_find_v2(v, lo, hi, e):
    fib = Fib(hi - lo)
    while lo < hi:
        while hi - lo < fib.get():
            fib.prev()
        mi = lo + fib.get() - 1
        if e < v[mi]:
            hi = mi
        elif e > v[mi]:
            lo = mi
        else:
            return mi
            
    return -1
   
 
class Fib(object):
    
    def __init__(self, n):
        
        self._f = 1
        self._g = 0
        while self._g < n:
            self.next()
            
    def next(self):
        self._g, self._f = self._g + self._f, self._g
        return self._g
        
    def prev(self):
        self._g, self._f = self._f, self._g - self._f
        return self._g
        
    def get(self):
        return self._g
        

v = [1, 2, 3, 4, 5, 6, 7]
print(bin_find_v2(v, 0, len(v), 5))

Python Tutor fib sort 演示

@xxleyi xxleyi moved this from In progress to Done in Programming: 从 SICP 到 Computation Model Feb 26, 2019
@xxleyi xxleyi added this to Done in 数据结构(C++) Feb 26, 2019
@xxleyi xxleyi closed this as completed Feb 27, 2019
@xxleyi xxleyi reopened this Apr 11, 2019
数据结构(C++) automation moved this from Done to In progress Apr 11, 2019
@xxleyi xxleyi added this to the 面试 milestone Sep 1, 2019
@xxleyi xxleyi mentioned this issue Sep 1, 2019
8 tasks
@xxleyi xxleyi added HOW and removed 有看头 labels Mar 28, 2020
@xxleyi xxleyi removed this from the 基础相关 milestone Sep 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
数据结构(C++)
  
In progress
Development

No branches or pull requests

1 participant