# 如何用python实现八大排序算法

python实现八大排序算法，具体内容如下

1、插入排序描述

def insert_sort(lists):
# 插入排序
count = len(lists)
for i in range(1, count):
key = lists[i]
j = i – 1
while j >= 0:
if lists[j] > key:
lists[j + 1] = lists[j]
lists[j] = key
j -= 1
return lists

2、希尔排序描述

def shell_sort(lists):
# 希尔排序
count = len(lists)
step = 2
group = count / step
while group > 0:
for i in range(0, group):
j = i + group
while j < count: k = j - group key = lists[j] while k >= 0:
if lists[k] > key:
lists[k + group] = lists[k]
lists[k] = key
k -= group
j += group
group /= step
return lists

3、冒泡排序描述

def bubble_sort(lists):
# 冒泡排序
count = len(lists)
for i in range(0, count):
for j in range(i + 1, count):
if lists[i] > lists[j]:
lists[i], lists[j] = lists[j], lists[i]
return lists

4、快速排序描述

def quick_sort(lists, left, right):
# 快速排序
if left >= right:
return lists
key = lists[left]
low = left
high = right
while left < right: while left < right and lists[right] >= key:
right -= 1
lists[left] = lists[right]
while left < right and lists[left] lists[j]: min = j lists[min], lists[i] = lists[i], lists[min] return lists

6、堆排序描述

# 调整堆
lchild = 2 * i + 1
rchild = 2 * i + 2
max = i
if i < size / 2: if lchild < size and lists[lchild] > lists[max]:
max = lchild
if rchild < size and lists[rchild] > lists[max]:
max = rchild
if max != i:
lists[max], lists[i] = lists[i], lists[max]
# 创建堆
def build_heap(lists, size):
for i in range(0, (size/2))[::-1]:
# 堆排序
def heap_sort(lists):
size = len(lists)
build_heap(lists, size)
for i in range(0, size)[::-1]:
lists[0], lists[i] = lists[i], lists[0]

7、归并排序描述

def merge(left, right):
i, j = 0, 0
result = []
while i < len(left) and j < len(right): if left[i]

Posted in 未分类