查找

1、二分查找

 1 def BinarySearch(list,left,right,key):  

 2     if left==right:

 3         if list[left]==key:

 4             return left

 5         else:

 6             return -1

 7     else:

 8         middle=math.ceil((left+right)/2)

 9         if list[middle]>key:

10            find= BinarySearch(list,left,middle-1,key)

11         else:

12            find= BinarySearch(list,middle,right,key)

13     return find
View Code

2、Hash查找

 1 def InsertHash(hashlist,hashlen,data):

 2     hashAddress=data%hashlen

 3     while hashlist[hashAddress]!=0:

 4         hashAddress+=1

 5         hashAddress=hashAddress%hashlen

 6     hashlist[hashAddress]=data

 7 

 8 def HashSearch(hashlist,hashlen,key):

 9     hashAddress=key%hashlen

10     while hashlist[hashAddress]!=0 and hashlist[hashAddress]!=key:

11         hashAddress+=1

12         hashAddress=hashAddress%hashlen

13     if hashlist[hashAddress]==0:

14         return -1

15     return hashAddress
View Code

 

你可能感兴趣的:(查找)