## 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] <= key:
left += 1
lists[right] = lists[left]
lists[right] = key
quick_sort(lists, low, left - 1)
quick_sort(lists, left + 1, high)
return lists``````

## 5、直接选择排序

### 代码实现

``````def select_sort(lists):
# 选择排序
count = len(lists)
for i in range(0, count):
min = i
for j in range(i + 1, count):
if lists[min] > 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] <= right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result += left[i:]
result += right[j:]
return result

def merge_sort(lists):
# 归并排序
if len(lists) <= 1:
return lists
num = len(lists) / 2
left = merge_sort(lists[:num])
right = merge_sort(lists[num:])
return merge(left, right)``````

## 8、基数排序

### 代码实现

``````import math
bucket = [[] for i in range(radix)]
for i in range(1, k+1):
for j in lists:
del lists[:]
for z in bucket:
lists += z
del z[:]
return lists``````

【Python Web学习路线】干货整理，不谈虚的，让你短时间高效学好它！

Eddie_yang
2018/12/24
0
0

01/07
0
0
Python之排序算法：快速排序与冒泡排序

Python之排序算法：快速排序与冒泡排序 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/7828610.html 　　入坑(简称IT)这一行也有些年头了，但自老师讲课提过排序算法后几乎再也没写过...

5è¬þxãÍ
2017/11/19
0
0

dby_freedom
2018/08/28
0
0
Reddit 的排名算法原理

oschina
2013/08/07
1K
1

JWT学习总结

56分钟前
3
0
AOP的学习（1）

AOP 理解AOP编程思想(面向方法、面向切面) spring AOP的概念 方面 -- 功能 目标 -- 原有方法 通知 -- 对原有方法增强的方法 连接点 -- 可以用来连接通知的地方（方法） 切入点 -- 将用来插入...

4
0

linsk1998

8
0
Python应用:python链表示例

python小白1

4
0
Source Insight加载源码

Source Insight是一个图形化的源代码查看工具（当然也可以作为编译工具）。如果一个项目的源代码较多，此工具可以很方便地查找到源代码自建的依赖关系。 1.创建工程 下图为Snort源代码的文件...

5
0