无序表查找

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)

python排序查找的更多相关文章

  1. Python二分查找算法

    Python 二分查找算法: 什么是二分查找,二分查找的解释: 二分查找又叫折半查找,二分查找应该属于减值技术的应用,所谓减值法,就是将原问题分成若干个子问题后,利用了规模为n的原问题的解与较小规模( ...

  2. python 排序算法总结及实例详解

    python 排序算法总结及实例详解 这篇文章主要介绍了python排序算法总结及实例详解的相关资料,需要的朋友可以参考下 总结了一下常见集中排序的算法 排序算法总结及实例详解"> 归 ...

  3. 带你掌握4种Python 排序算法

    摘要:在编程里,排序是一个重要算法,它可以帮助我们更快.更容易地定位数据.在这篇文章中,我们将使用排序算法分类器对我们的数组进行排序,了解它们是如何工作的. 本文分享自华为云社区<Python ...

  4. python排序之二冒泡排序法

    python排序之二冒泡排序法 如果你理解之前的插入排序法那冒泡排序法就很容易理解,冒泡排序是两个两个以向后位移的方式比较大小在互换的过程好了不多了先上代码吧如下: 首先还是一个无序列表lis,老规矩 ...

  5. python排序之一插入排序

    python排序之一插入排序 首先什么是插入排序,个人理解就是拿队列中的一个元素与其之前的元素一一做比较交根据大小换位置的过程好了我们先来看看代码 首先就是一个无序的列表先打印它好让排序后有对比效果, ...

  6. 用 Python 排序数据的多种方法

    用 Python 排序数据的多种方法 目录 [Python HOWTOs系列]排序 Python 列表有内置就地排序的方法 list.sort(),此外还有一个内置的 sorted() 函数将一个可迭 ...

  7. Sublime文本排序&查找重复行&删除重复行

    排序 按F9或者选择菜单:Edit > Sort Lines,对每行文本进行排序 查找重复行 排序好后,按Ctrl+F,调出查找面板 查找字符串: ^(.+)$[\r\n](^\1$[\r\n] ...

  8. python 字符串查找

    python 字符串查找有4个方法,1 find,2 index方法,3 rfind方法,4 rindex方法. 1 find()方法: )##从下标1开始,查找在字符串里第一个出现的子串:返回结果3 ...

  9. Java进阶(三十九)Java集合类的排序,查找,替换操作

    Java进阶(三十九)Java集合类的排序,查找,替换操作 前言 在Java方向校招过程中,经常会遇到将输入转换为数组的情况,而我们通常使用ArrayList来表示动态数组.获取到ArrayList对 ...

随机推荐

  1. Android Okhttp完美同步持久Cookie实现免登录

    通过对Retrofit2.0的<Retrofit 2.0 超能实践,完美支持Https传输>基础入门和案例实践,掌握了怎么样使用Retrofit访问网络,加入自定义header,包括加入S ...

  2. Git提交.net项目的小问题

    今天早上写了点关于asp.net core授权的东西,输入git add .的时候出现的报错 $ git add .error: open(".vs/DOTNETAuthorization/ ...

  3. BZOJ1014[JSOI2008]火星人prefix(splay维护hash)

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  4. COGS 163 [USACO Mat07] 牛语

    COGS 163 [USACO Mat07] 牛语 输入文件:latin.in   输出文件:latin.out   简单对比 时间限制:1 s   内存限制:128 MB 奶牛们听说猪发明了一种秘密 ...

  5. Apache httpd.conf 配置文件语法验证

    Apache 的 httpd.conf文件改动之后,必须重新启动server才干生效. 有时server在提供服务的时候,直接更改配置,重新启动服务.会带来非常大的危急性. 假设能在改动配置之后,先验 ...

  6. php中 重载(二)

    接着上一次说的重载,我们了解下php中的重载,方法的重载,假设有管重载定义,參考:php中 重载(一)这个文章,谢谢.作为刚開始学习的人,大牛勿喷: 基本是两个方法 __call,当调用对一个不可訪问 ...

  7. POJ Oulipo(KMP模板题)

    题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #in ...

  8. windows 批处理脚本(batch scripting)

    Guide to Windows Batch Scripting DOS 不需对变量事先声明.未声明或未初始化变量是一个空字符串("") 1. 变量赋值 set命令用于变量赋值.s ...

  9. DriverModule_01

    最小驱动模块: 最简单的Makefile 无配置文件 最小驱动的四部分 头文件 声明模块信息 模块驱动的入口.出口 功能区 关于这个头文件的分析: linux头文件的位置,例如#include< ...

  10. python关于字典的操作

    https://www.cnblogs.com/RENQIWEI1995/p/7931374.html 最常用的代码举例: dict = {'Name': 'Zara', 'Age': 7, 'Cla ...