1. 程式人生 > >簡單算法

簡單算法

turn 應用層 item abcd test .html port on() html

網絡
物理層
數據鏈路層
網絡層
傳輸層
應用層

def binary_search(lis,item):
low = 0
high = len(lis)-1
if low <= high:
mid = (low+high)/2
if lis[mid]<item:
low = mid+1
elif lis[mid]>item:
high = mid -1
else:
return mid
return None

mylist = [1,3,5,7]
print(binary_search(mylist,3))


def bubble_sort(mylist):
for i in range(0,len(mylist)):
for j in range(0,len(mylist)-1):
if mylist[j] > mylist[j+1]:
mylist[j],mylist[j+1] = mylist[j+1],mylist[j]
return mylist
a=[1,77,23,2]
print bubble_sort(a)


def quick_sort(mylist):
if len(mylist)<2:
return mylist
else:
privot=mylist[0]
less = [i for i in mylist[1:] if i<privot]
greater = [i for i in mylist[1:] if i>privot]
return quick_sort(less)+[privot]+quick_sort(greater)
print quick_sort([34,31,22,1])

單例:
#test.py
class my_singleton(object):
def test(self):
pass
singleton = my_singleton()
#to use
from test import singleton
singleton.test()


a = "abcd"
print a[::-1]
print ‘‘.join(a[i] for i in range(len(a)-1,-1,-1))

microcode_ctl Intel系列的CPU可以通過這個服務支持額外的微指令集

http://blog.sina.com.cn/s/blog_1554fae900102xfmn.html

簡單算法