常见算法Python实现

1排序

1.1选择法排序

'''
第i轮排序完后,第i位的数字是之后所有数中的最小数
'''
def selectsort(lst,reverse=False):
	for i in range(len(lst)):
		m = i
		for j in range(i+1,len(lst)):
			exp = 'lst[m] > lst[j]'
			if reverse:
				exp = 'lst[m] < lst[j]'
			if eval(exp):
				m = j
		if m != i:
			lst[m],lst[i] = lst[i], lst[m]
		print(lst)	


lst = [8,4,6,2,4,8,2]
selectsort(lst,True)


猜你喜欢

转载自blog.csdn.net/baidu_41867252/article/details/86517886