查找(Python)

查找

  • 顺序查找
  • 二分查找
  • 哈希查找

1、顺序查找

1.1、无序

def sequentialSearch(alist, target):
    pos = 0
    found = False
    while pos < len(alist) and not found:
        if alist[pos] == target:
            found = True
        else:
            pos += 1
    return found
sequentialSearch([1,19,20,4,5,5,45,32],32)
True
sequentialSearch([1,19,20,4,5,5,45,32],12)
False

1.2、有序

def orderedSearch(alist, target):
    found = False
    stop = False
    pos = 0
    while pos < len(alist) and not found and not stop:
        if alist[pos] == target:
            found = True
        else:
            if alist[pos] > target:
                stop = True
                print("stopped at: ", pos)
            else:
                pos += 1
    return found
orderedSearch([1,4,7,9,13,49,50,99,130], 13)
True
orderedSearch([1,4,7,9,13,49,50,99,130], 48)
stopped at:  5





False
orderedSearch([1,4,7,9,13,49,50,99,130], 130)
True

2、二分查找

def binerySerch(alist, target):
    found = False
    front = 0
    last = len(alist) - 1
    while front <= last and not found:
        mid = (front + last) // 2
        if alist[mid] == target:
            found = True
        else:
            if alist[mid] > target:
                last = mid -1
            else:
                front = mid + 1
    return found
binerySerch([1,2,3,4,7,8,9], 6)
False
binerySerch([1,2,3,4,7,8,9], 9)
True
binerySerch([1,2,3,4,7,8,9], 4)
True

哈希查找

def Hash(strs, L):
    sum = 0
    for ch in strs:
        sum += ord(ch)
    return sum%L
Hash('cat', 11)
4
class HashTable:
    def __init__(self):
        self.size = 11
        self.slots = [None] * self.size
        self.data =  [None] * self.size
    
    def put(self, key, data):
        hashvalue = self.hashfunction(key, self.size)
        if self.slots[hashvalue] == None:
            self.data[hashvalue] = data
            self.slots[hashvalue] = key
        else:
            if self.slots[hashvalue] == key:
                self.data[hashvalue] = data
            else:
                nextslot = self.rehash(hashvalue, self.size)
                while self.slots[nextslot] !=None and self.slots[nextslot] != key:
                    nextslot = self.rehash(nextslot, self.size)
                if self.slots[nextslot] == None:
                    self.slots[nextslot] = key
                    self.data[nextslot]  = data
                else:
                    self.data[nextslot] = data
                    
    def hashfunction(self, key, size):
        return key % size
    
    def rehash(self, oldhash, size):
        return (oldhash + 1) % size
    
    
    def get(self, key):
        startslot = self.hashfunction(key, self.size)
        data = None
        stop = False
        found = False
        position = startslot
        
        while self.slots[position] != None and not found and not stop:
            if self.slots[position] == key:
                found = True
                data = self.data[position]
            else:
                position = self.rehash( position, self.size)
                if position == startslot:
                    stop = True
        return data
    
    def __getitem__(self, key):
        return self.get(key)
    def __setitem__(self, key,data):
        self.put(key,data)
H = HashTable()
H[54] = 'cat'
H[26] = 'dog'
H[93] = 'lion'
H[17] = 'tiger'
H[77] = 'bird'
H[31] = 'cow'
H[44] = 'goat'
H[55] = 'pig'
H[20] = 'chicken'
print(H.slots)
print(H.data)
[77, 44, 55, 20, 26, 93, 17, None, None, 31, 54]
['bird', 'goat', 'pig', 'chicken', 'dog', 'lion', 'tiger', None, None, 'cow', 'cat']
print(list(zip(H.slots, H.data)))
[(77, 'bird'), (44, 'goat'), (55, 'pig'), (20, 'chicken'), (26, 'dog'), (93, 'lion'), (17, 'tiger'), (None, None), (None, None), (31, 'cow'), (54, 'cat')]
H[20]
'chicken'
H[20] = 'duck'
H[20]
'duck'
print(H.data)
['bird', 'goat', 'pig', 'duck', 'dog', 'lion', 'tiger', None, None, 'cow', 'cat']

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值