无序表查找

def seq_search(lst, key):
found = False
pos = 0
while pos < len(lst) and not found:
if lst[pos] == key:
found = True
else:
pos = pos + 1
return found

顺序表二分查找

def ordered_seq_search(lst, key):
if len(lst) == 0:
return -1
mid = len(lst) // 2
if lst[mid] == key:
return mid
else:
if key < lst[mid]:
return ordered_seq_search(lst[:mid], key)
else:
return ordered_seq_search(lst[mid + 1:], key)

Hash

def hashlibTest():
import hashlib
str = "Hello,World"
print(hashlib.md5(str).hexdigest())
print(hashlib.sha1(str).hexdigest())
print(hashlib.sha224(str).hexdigest())
print(hashlib.sha256(str).hexdigest())
print(hashlib.sha512(str).hexdigest())

简单排序

def bubbleSort(lst):
for i in range(len(lst)):
for j in range(len(lst) - 1, i, -1):
if lst[j] < lst[j - 1]:
lst[j], lst[j - 1] = lst[j - 1], lst[j] def selectSort(lst):
'''
从后向前,从待排序部分选择,每一趟外层选择,确定最大元素位置,进行一次交换
:param lst:
:return:
'''
for fillslot in range(len(lst) - 1, 0, -1):
positionOfMax = 0
for location in range(1, fillslot + 1):
if lst[location] > lst[fillslot]:
positionOfMax = location
lst[positionOfMax], lst[fillslot] = lst[fillslot], lst[positionOfMax] def insertSort(lst):
for i in range(1, len(lst)):
currentValue = lst[i]
position = i
while position > 0 and currentValue < lst[position - 1]:
lst[position] = lst[position - 1]
position = position - 1
lst[position] = currentValue def mergeSort(lst):
if len(lst) > 1:
mid = len(lst) // 2
lefthalf = lst[:mid]
righthalf = lst[mid:] # 递归
mergeSort(lefthalf)
mergeSort(righthalf) i = j = k = 0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
lst[k] = lefthalf[i]
i = i + 1
else:
lst[k] = righthalf[j]
j = j + 1
k = k + 1
while i < len(lefthalf):
lst[k] = lefthalf[i]
i = i + 1
k = k + 1
while j < len(righthalf):
lst[k] = righthalf[j]
j = j + 1
k = k + 1 def quickSort(lst, left, right):
if left < right:
i = left
j = right
k = lst[i]
while i < j:
while i < j and lst[j] > k:
j = j - 1
lst[i] = lst[j]
while i < j and lst[i] < k:
i = i + 1
lst[j] = lst[i]
lst[i] = k
quickSort(lst, left, i - 1)
quickSort(lst, i + 1, right) if __name__ == '__main__':
seq = [x for x in range(1, 11)]
# print(seq_search(seq, 8))
# print(seq_search(seq, 18))
# print(ordered_seq_search(seq, 8))
# print(ordered_seq_search(seq, 18)) # hashlibTest() lst = [2, 0, 3, 6, 1, 4, 9, 7, 5, 8]
quickSort(lst, 0, len(lst) - 1)
print(lst)

最新文章

  1. grub.conf文件参数详解
  2. Hibernate查询语句
  3. codeforces 519E A and B and Lecture Rooms LCA倍增
  4. iOS开发UI篇—popoverController简单介绍
  5. iOS-UIButton-设置button标题和图片位置
  6. MasterPage 变化了的 ClientID ctl00_
  7. 项目源码--Android基于LBS地理位置信息应用的客户端
  8. opencv之图像膨胀
  9. python-property、docstring--笔记
  10. WebApi 能支持Session
  11. Django:之不得不说的web框架们
  12. Tessnet2图片识别(2)
  13. 如何使用webapi集成swagger
  14. JSR-303校验类型
  15. windows的MySQL安装
  16. Netty未来展望
  17. 【eclipse】eclipse报错:the resource is not on the build path of a java project
  18. 性能调优6:Spool 假脱机调优
  19. 类ThreadLocal的使用与源码分析
  20. 1安装Linux

热门文章

  1. Android使用蓝牙连接adb调试App
  2. Linux下SPI读写外部寄存器的操作
  3. 23. Node.Js Buffer类(缓冲区)-(三)文件读取实例
  4. BZOJ 1009 GT考试 (AC自动机 + 矩阵乘法加速dp)
  5. 团队作业-Beta冲刺(2)
  6. IIS特殊字符设置
  7. django-rest-framework框架 第四篇 认证Authentication
  8. AAC编解码
  9. 关于laravel框架分页报错的问题
  10. 查看网站使用何种框架或者技术的插件——Wappalyzer