# 令牌桶算法和漏桶算法python_Python 排序算法

O（c）-常量时间复杂度-布隆过滤器/哈希存储

O（log2n : 注 :‘2’为底数）-对数时间复杂度-折半查找（二分查找）

O（n）-线性时间复杂度-顺序查找/桶排序

O（n*log2n : 注 :‘2’为底数）-对数线性时间复杂度-高级排序算法（归并排序/快速排序）

O（n^2）-平方时间复杂度-简单排序算法（选择排序、插入排序、冒泡排序）

O（n^3）-立方时间复杂度-Floyd算法/矩阵乘法运算

O（2^n）-几何级数时间复杂度-汉诺塔

O（n!）-阶乘时间复杂度-旅行经销商问题-NP

"""简单选择排序"""
def select_sort(origin_items, comp=lambda x, y: x < y):
items = origin_items
for i in range(len(items) - 1):
min_index = i
for j in range(i + 1, len(items)):
if comp(items[j], items[min_index]):
min_index = j
items[i], items[min_index] = items[min_index], items[i]
return items

def main():
list1 = [3, 1, 6, 2]
print(select_sort(list1))

if __name__ == '__main__':
main()

"""插入排序"""
def insert_sort(items):

for i in range(1, len(items)):
key = items[i]
j = i - 1
while j >= 0 and key < items[j]:
items[j + 1] = items[j]
j -= 1
items[j + 1] = key
return items

def main():
list1 = [5, 11, 7, 10, 13, 6]
print(insert_sort(list1))

if __name__ == '__main__':
main()

"""冒泡排序"""
def bubble_sort(origin_items, comp=lambda x, y: x > y ):
items = origin_items[:]
for i in range(len(items) - 1):
flag = 1
for j in range(len(items)- 1 - i):
if comp(items[j], items[j + 1]):
items[j], items[j + 1] = items[j + 1], items[j]
flag = 0
if flag:
break
return items
def main():
list1 = [6, 1, 2, 3]
print(bubble_sort(list1))

if __name__ == '__main__':
main()

"""高质量冒泡排序（搅拌排序）"""
def bubble_sort(origin_items, comp=lambda x, y: x > y):
items = origin_items[:]
for i in range(len(items) - 1):
swapped = False
#将大值放于右边
for j in range(i, len(items) - 1 - i):
if comp(items[j], items[j + 1]):
items[j], items[j + 1] = items[j + 1], items[j]
swapped = True
# 将小值放于左边
if swapped:
swapped = False
for j in range(len(items) - 2 - i, i, -1):
if comp(items[j - 1], items[j]):
items[j - 1], items[j] = items[j], items[j - 1]
swapped = True
#跳出循环
if not swapped:
break
return items

def main():
list1 = [6, 3, 2, 1]
print(bubble_sort(list1))

if __name__ == '__main__':
main()

"""归并排序（分治法）"""
def merge_sort(items):
if len(items) < 2:
return items
mid = len(items) // 2
left = merge_sort(items[:mid])
right = merge_sort(items[mid:])
return merge(left, right)

def merge(items1, items2, comp=lambda x, y: x <= y):
"""合并（将两个有序的列表合并成一个有序的列表）"""
items = []
index1, index2 = 0, 0
while index1 < len(items1 ) and index2 < len(items2):
if comp(items1[index1], items2[index2]):
items.append(items1[index1])
index1 += 1
else:
items.append(items2[index2])
index2 += 1
items += items1[index1:]
items += items2[index2:]
return items

def main():
list1 = [3, 1, 6, 2, 7, 9, 4, 2]
print(merge_sort(list1))

if __name__ == '__main__':
main()

# def partition(items, start, end, comp=lambda x, y: x <= y):
def partition(items, start, end):
"""划分"""
pivot = items[end]
i = start - 1
for j in range(start, end):
if items[j] <= pivot:
# if comp(items[j], pivot):
i += 1
items[i], items[j] = items[j], items[i]
items[i + 1], items[end] = items[end], items[i + 1]
return (i + 1)

def quick_sort(items,start, end):
print('items', items)
if start < end:
pos = partition(items, start, end)

quick_sort(items, start, pos - 1)
quick_sort(items, pos + 1, end)
return items
def main():
# list1 = [4, 2, 5]
list1 = [4, 2, 7, 6, 1, 5]
print(quick_sort(list1, 0, len(list1) - 1))

if __name__ == '__main__':
main()

"""顺序查找"""
def seq_search(items, key):
for index, item in enumerate(items):
if item == key:
return index
return -1

def main():
list1 = [3, 1, 6, 2, 7, 9, 4, 2]
key = int(input('input the key:'))
print(seq_search(list1, key))

if __name__ == '__main__':
main()

"""折半查找"""
def bin_search(items, key):
start, end = 0, len(items) - 1
while start <= end:
mid = (start + end) // 2
if key > items[mid]:
start = mid + 1
elif key < items[mid]:
end = mid - 1
else:
return mid

def main():
list1 = [3, 5, 6, 7, 9, 10, 12]
key = int(input('input the key:'))
print(bin_search(list1, key))

if __name__ == '__main__':
main()

08-23 2万+
08-11 665
12-08 16
10-16 77
08-20 203
01-29
©️2020 CSDN 皮肤主题: 1024 设计师:白松林