python选择排序的实现
代码:
from random import randrange,shuffle
def Choicesort():
arry=[]
while len(arry)<12:
arry.append(randrange(-199,199,5))
shuffle(arry)
print("选择排序前的数组:{}".format(arry))
for i in range(11):
for j in range(i+1,12):
if arry[i]>arry[j]:
temp=arry[i]
arry[i]=arry[j]
arry[j]=temp
print("选择排序后的数组:{}".format(arry))
Choicesort()
结果:
选择排序前的数组:[91, -84, -69, -169, 181, -139, -199, -119, 181, 71, 81, -74]
选择排序后的数组:[-199, -169, -139, -119, -84, -74, -69, 71, 81, 91, 181, 181]
如有不对之处,请多多读指教