简单说明:
1.冒泡排序:双层循环,交替结果
2.选择排序:while nums,假设第一个值为做小,通过for循环找到最小值以此来替换,再将nums中该值去掉继续上述步骤
3.快速排序:定义一个初值,把整个数据列表分为两部分,再递归
代码实现:
# 冒泡排序
def action1(n):
for i in range(len(n)):
for j in range(len(n)):
if n[i] < n [j]:
tmp = n[i]
n[i] = n[j]
n[j] = tmp
print(n)
# 选择排序
def choice_order(n):
sm = n[0]
si = 0
for i in range(len(n)):
if n[i] < sm:
sm = n[i]
return sm
def action2(n):
rs = []
while n:
res = choice_order(n)
rs.append(res)
n.remove(res)
print(rs)
# 快速排序
def action3(n):
if len(n) < 2:
return n
num = n[0]
less = [i for i in n if i < num]
more = [i for i in n if i > num]
return action3(less) + [num] + action3(more)
if __name__ == '__main__':
nums = [1, 6, 3, 2, 7]
# 冒泡排序
action1(nums)
# 选择排序
action2(nums)
# 快速排序
res = action3(nums)
print(res)