python顺序查找排序_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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值