def find_l_aux(start,end,find):
'递归找左边界'
m=int((start+end)/2)
if start==end:
return 0
elif a[m]==find:
return find_l_aux(start,m,find)
elif a[m+1]==find:
return m+1
elif a[m]<find:
return find_l_aux(m,end,find)
def find_r_aux(start,end,find):
'递归找右边界'
m=int((start+end)/2)
if a[m]==find:
if m+1==len(a) or a[m+1]!=find:
return m+1
return find_r_aux(m,end,find)
elif a[m]>find:
return find_r_aux(start,m,find)
def find(find):
'''
二分定位
'''
if len(a)==1 and a[0]==find:
return 1
elif len(a)==1 and a[0]!=find:
return 0
low=0
high=len(a)
l=high-1
mid=0
while low<=high and mid<l:
mid=int((low+high)/2)
if a[mid]==find:
ffl=find_l_aux(0,mid,find)
ffr=find_r_aux(mid,len(a),find)
r=ffr-ffl
return r
if a[mid]>find:
high=mid-1
if a[mid]<find:
low=mid+1
return 0
if __name__ == '__main__':
a=[11,11,122,122,122,122,1444,1444]
for i in set(a):
print('{}出现过{}次'.format(i,find(i)))