python排序查找

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python排序查找相关的知识,希望对你有一定的参考价值。

无序表查找

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排序查找的主要内容,如果未能解决你的问题,请参考以下文章

以下代码片段的时间复杂度是多少?

python2根据索引表查找相应的录音片段拼接录音

Python数据结构系列☀️《查找排序-基础知识》——知识点讲解+代码实现☀️

从搜索文档中查找最小片段的算法?

二分查找和快速排序(理论+代码)

python查找与排序算法详解(示意图+代码看完基础不成问题)