python手写排序函数 在线等
发布网友
发布时间:2022-04-24 00:00
我来回答
共1个回答
热心网友
时间:2022-04-18 06:58
#举个例子,冒泡排序
def sort_list(f, l):
print "befor:", l
for i in range(len(l)):
for j in range(i, len(l)):
if not f(l[j], l[i]):
l[i], l[j] = l[j], l[i]
print "sorted: ", l
#function
f = lambda a,b : a>= b
#test
l1 = [3,2,1,6,4]
l2 = ["a","x", 5, "g"]
sort_list(f, l1)
sort_list(f, l2)
#STDOUT:
"""
befor: [3, 2, 1, 6, 4]
sorted: [1, 2, 3, 4, 6]
befor: ['a', 'x', 5, 'g']
sorted: [5, 'a', 'g', 'x']
"""