counts = [98,12,3,4,1,4,9,3821]
minNum = min(counts)
#print minNum
minNum_index = counts.index(minNum)
#print minNum_index #找出列表中最小的2个元素
def find_two_smallest(L):
smallest = min(L)
min_index = L.index(smallest)
L.remove(smallest) smallest2 = min(L)
min_index2 = L.index(smallest2)
L.remove(smallest2) L.insert(min_index,smallest)
L.insert(min_index2,smallest2) if min_index <= min_index2:
min_index2 +=1 return (smallest,smallest2) ansList = find_two_smallest(counts)
print ansList #第二种方式
def find_two_smallest2(L):
tempList = L[:]
tempList.sort()
return (tempList[0],tempList[1]) ansList2 = find_two_smallest2(counts)
print ansList2 #搜索
def linear_search(v,L):
i = 0
while i<len(L) and L[i] != v:
i = i+1
return i print linear_search(7,counts)
print linear_search(12,counts) #计算花费的时间
import time
def linear_search2(v,L):
i = 0
for value in L:
if value == v:
return i
i = i+1
return len(L) L = range(1000001)
time1 = time.time()
linear_search(7,L)
linear_search(500000,L)
time2 = time.time()
print (time2-time1)*1000 L = range(10)
print L #二分搜索
def binary_search(v,L):
i = 0
j = len(L) -1
while i <= j:
m = (i+j)/2
if(L[m]<v):
i = m +1
else:
j = m - 1
if 0 <= i <len(L) and L[i] == v:
return i
else:
return -1 #测试
VALUES = [1,3,4,6,8,9,10]
assert binary_search(1,VALUES) == 0
assert binary_search(2,VALUES) == -1

最新文章

  1. maven阿里云中央仓库
  2. jquer 事件,选择器,dom操作
  3. 跟我学习dubbo-Dubbo管理控制台的安装(3)
  4. 10位顶级PHP大师的开发原则
  5. 从对偶问题到KKT条件
  6. Tomcat 7 Connector 精读(2) CoyoteAdapter
  7. http://poj.org/problem?id=2253
  8. 常见sql的error解决方法
  9. git 查看文件修改记录
  10. ADO.NET DataReader和DataAdapter的区别
  11. java的访问控制(包、访问修饰符、修饰符)
  12. ubuntu12.04 安装和配置jdk1.7
  13. ubuntu安装nVidia驱动,遇到终端闪砾问题并解决
  14. 框架学习:ibatis框架的结构和分析
  15. Python【第四篇】函数、内置函数、递归、装饰器、生成器和迭代器
  16. hashtable and hashmap
  17. oracle undo表空间
  18. Codeforces Round #373 (Div. 2) C. Efim and Strange Grade 水题
  19. SSO单点登录之Asp.Net实现示例
  20. 解决安装Egit时Egit Mylyn和org.eclipse.team.core报错

热门文章

  1. 【线段树分治 01背包】loj#6515. 「雅礼集训 2018 Day10」贪玩蓝月
  2. 【数学 技巧】divisor
  3. nodejs开发过程中遇到的一些插件记录
  4. spring事务(Transaction )报 marked as rollback-only异常的原因及解决方法
  5. atag信息处理
  6. 可持久化treap(FHQ treap)
  7. BZOJ 5064: B-number
  8. 【Codeforces Round #476 (Div. 2) [Thanks, Telegram!] C】Greedy Arkady
  9. .NET开发时让人头痛的SESSION超时
  10. Django中间件、Auth认证