Python二分查找詳解
先來看個實例
#!/usr/bin/env python
import sys
def search2(a,m):
low = 0
high = len(a) - 1
while(low <= high):
mid = (low + high)/2
midval = a[mid]
if midval < m:
low = mid + 1
elif midval > m:
high = mid - 1
else:
print mid
return mid
print -1
return -1
if __name__ == "__main__":
a = [int(i) for i in list(sys.argv[1])]
m = int(sys.argv[2])
search2(a,m)
運行:
administrator@ubuntu:~/Python$ python test_search2.py 123456789 4
3
注:
1.'__':由于python的類成員都是公有、公開的被存取public,缺少像正統(tǒng)面向?qū)ο笳Z言的私有private屬性。
于是就用__來將就一下,模擬私有屬性。這些__屬性往往是內(nèi)部使用,通常情況下不用改寫。也不用讀取。
加上2個下劃線的目的,一是不和普通公有屬性重名沖突,二是不讓對象的使用者(非開發(fā)者)隨意使用。
2.__name__ == "__main__"表示程序腳本是直接被執(zhí)行的.
如果不等于表示腳本是被其他程序用import引入的.則其__name__屬性被設(shè)為模塊名
Python采用二分查找找出數(shù)字的下標(biāo)
要考慮有重復(fù)數(shù)字的情況
class Solution(object):
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
def binary_search(start,end,value):
while end>=start:
mid = (start+end)//2
print(mid)
if nums[mid]>target:
end = mid-1
elif nums[mid]<target:
start = mid+1
else:
if value==-1:
if mid-1>=start and nums[mid+value] == target:
end = mid+value
else:
return mid
else:
if mid+1<=end and nums[mid+value] == target:
start = mid+value
else:
return mid
return -1
a=binary_search(0,len(nums)-1,-1)
b=binary_search(0,len(nums)-1,1)
return [a,b]
a = Solution()
l = [2,2]
print(a.searchRange(l,2))
二分算法的定義不在多說了,百度一下就知道(支持國產(chǎn)大笑)
import sys
source = [1,2,3,4,5,6,7,8,9,10] #must be in order
des = int(sys.argv[1])
low = 0
high = len(source) - 1
targetIndex = -1
print "des=",des
while low <= high:
middle = (low + high)/2
if des == source[middle]:
targetIndex = middle
break
elif des < source[middle]:
high = middle -1
print "middle element[index=",middle,",value=",source[middle],"] is bigger than des, continue search from[",low,"to",high,"]"
else:
low = middle + 1
print "middle element[index=",middle,",value=",source[middle],"] is smaller than des, continue search from[",low,"to",high,"]"
print "search complete, target element's index in source list is ",targetIndex
最后在分享一個
'fileName--BinarySearch.py'
src = []
def BinarySearch(low, high, target, *src):
'二分查找'
while low <= high:
mid = (low + high) // 2
midVal = src[mid]
if target < midVal:
high = mid - 1
elif target > midVal:
low = mid + 1
else:
return mid
BinarySearch(low, high, target, *src)
print('Please input 10 number:')
for number in range(10):
src.append(int(input('Num %d:' % number)))
sortList = tuple(src)
key = int(input('Please input key:'))
location = BinarySearch(0, len(src) - 1, key, *sortList)
if location != None:
print('Find target at %d' % (location + 1))
else:
print('No target!')
相關(guān)文章
用python按照圖像灰度值統(tǒng)計并篩選圖片的操作(PIL,shutil,os)
這篇文章主要介紹了用python按照圖像灰度值統(tǒng)計并篩選圖片的操作(PIL,shutil,os),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Keras使用tensorboard顯示訓(xùn)練過程的實例
今天小編就為大家分享一篇Keras使用tensorboard顯示訓(xùn)練過程的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
python反射機制內(nèi)置函數(shù)及場景構(gòu)造詳解
這篇文章主要為大家介紹了python反射機制內(nèi)置函數(shù)及場景構(gòu)造示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
centos 安裝Python3 及對應(yīng)的pip教程詳解
這篇文章主要介紹了centos 安裝Python3 及對應(yīng)的pip的教程,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06
Matplotlib繪圖基礎(chǔ)之配置參數(shù)詳解
Matplotlib?提供了大量配置參數(shù),這些參數(shù)可以但不限于讓我們從整體上調(diào)整通過?Matplotlib?繪制的圖形樣式,下面我們就來看看如何巧妙的運用這些參數(shù)吧2023-08-08

