python排序查找
2017-10-02 15:34
211 查看
无序表查找
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)
相关文章推荐
- leetcode—217. Contains Duplicate 查找双数,python排序sort、sorted,for遍历的范围
- 用python实现二叉树搜索树/查找树/排序树的验证(判断)(四)(表插入递归)
- 005_009 Python 在排序已经完成的序列中查找元素
- python算法分析---查找和排序总结
- Python数据结构——查找和排序
- python3 -- 列表操作(排序 查找 清空 反转 计数 扩展)
- Python 递归函数选择排序和二分查找
- python 中 list 列表 的十种操作方法:添加,插入,弹出,删除,延长,运算,查找,排序,反转,采用递归函数深度遍历list
- Python中的列表(添加二元列表、连接、查找、排序、反序、赋值、删除、分片操作、负索引、)
- Python的查找和排序小程序
- 排序和搜索(二)——python实现二分查找
- python小程序:查找nginx日志文件的ip点击率并排序
- python数据结构与算法27 排序与查找 顺序查找
- python中实现二分查找,插入排序,归并排序,快速排序
- 排序和搜索(一)——python实现顺序查找
- python 排序算法之 二分查找
- python---实战日志文件中查找特定字段,文件操作读写、排序、替换、列表、集合
- 用python实现二叉树搜索树/查找树/排序树的验证(判断)(二)(列队宽度遍历)
- python---语句与数据结构应用,迭代、迭代字典、字典排序、字典根据键值查找键、sorted排序方法、translate与maketrans、with语句
- python 系统学习笔记(十四)---排序