# python之查找（顺序查找、折半查找、索引查找、二叉排序树查找、哈希表查找）

## 查找

### 静态查找

顺序查找



#### 顺序查找

python实现：

class StaticTable:
def __init__(self):
self.SequenceList = []
def CreateStaticTable(self,elements):
self.SequenceList = elements
'''顺序查找'''
def SequenceSearch(self,key):
iPos = -1
for i in range(len(elements)):
if self.SequenceList[i] == key:
iPos = i
break
return iPos
if __name__ == '__main__':
elements=[3,5,8,123,22,54,7,99,300,222]
table1 = StaticTable()
table1.CreateStaticTable(elements)
iPos = table1.SequenceSearch(99)
print("位置为：%d"  %iPos)

#### 折半查找

python实现：

class StaticTable:
def __init__(self):
self.BinaryList = []
def CreateStaticTable(self,elements):
self.BinaryList = elements
'''折半查找'''
def BinarySearch(self,key):
low = 0
high = len(elements)-1
while low <= high:
mid = (high+low)//2
if key > self.BinaryList[mid]:
low = mid + 1
elif key < self.BinaryList[mid]:
high = mid - 1
else:
return mid
return -1
if __name__ == '__main__':
elements = [3,5,8,22,54,67,99,107,222,300]
table1 = StaticTable()
table1.CreateStaticTable(elements)
iPos = table1.BinarySearch(99)
print("位置为：%d"  %iPos)    

#### 索引查找

python实现：

class IndexTable:
def __init__(self):
self.data = []
self.key = []
def CreatIndexTable(self,elements,n,s):
t = n // s;
for i in range (0,len(elements),t):
self.data.append(i)
self.key.append(max(elements[i:i+t]))
return self.data,self.key
class StaticTable:
def __init__(self):
self.SequenceList = []
def CreateStaticTable(self,elements):
self.SequenceList= elements
def IndexSearch(self,key,FirstPosList,MaxValueList):
iPos = -1
low = 0
high = 0
num = FirstPosList[1] - FirstPosList[0]
for i  in range(len
• 4
点赞
• 24
收藏
觉得还不错? 一键收藏
• 0
评论
07-01 2150
03-02 487
09-22 253
07-03
09-21
06-23
07-23 174
06-29 1189
10-30 2845
12-11 1万+
04-11 409

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

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

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