import os, sys
def swap(a, b):
a, b = b, a;
return a, b
def select_sort(seq, lenth):
for i in range(0, lenth):
pos = i
for j in range(i+1, lenth):
if seq[pos] > seq[j]:
pos = j
if i != pos:
seq[pos], seq[i] = swap(seq[pos], seq[i])
def insert_sort(seq, lenth):
for i in range(1, lenth):
tmp = seq[i]
j = i
while j > 0 and seq[j-1] > tmp:
# for j in range(i, -1, -1):
if seq[j-1] > tmp:
seq[j] = seq[j-1]
j-=1
seq[j] = tmp
return seq
def bubble_sort(seq, lenth):
if lenth < 2:
return seq
for i in range(lenth):
flag = True
for j in range(1, lenth - i):
if seq[j] < seq[j-1]:
seq[j], seq[j-1] = swap(seq[j], seq[j-1])
flag = False
if flag == True:
break
return seq
def quick_sort(seq, lenth):
if __name__ == "__main__":
a = [4, 2, 1, 3, 6, 8, 7]
lenth = len(a)
select_sort(a, lenth)
print a
简单排序算法的python实现
最新推荐文章于 2020-04-01 21:28:45 发布