python:输入随机数组,两两交换,使其从小到大排序
作者:互联网
这个是对数组应用的检测,以下是代码部分:
x_list = 0
y_list = 1
jump_num = 0
rounds = 0
num = input('请输入数组(以空格隔开):')
num_list = [int(n) for n in num.split()]
print('你输入的数组:',num_list)
while True:
while y_list < len(num_list):
if num_list[x_list] <= num_list[y_list]:
x_list += 1
y_list += 1
elif num_list[x_list] > num_list[y_list]:
tmp = num_list[x_list]
num_list[x_list] = num_list[y_list]
num_list[y_list] = tmp
x_list += 1
y_list += 1
if y_list == len(num_list):
x_list = 0
y_list = 1
rounds += 1
print('第%d轮为:'%rounds,num_list)
while True:
if y_list == len(num_list) :
jump_num = 1
break
elif num_list[x_list] <= num_list[y_list]:
x_list += 1
y_list += 1
elif num_list[x_list] > num_list[y_list]:
x_list = 0
y_list = 1
break
if jump_num == 1:
print('经过%d轮,最终为:'%rounds,num_list)
break
以下是结果:
标签:elif,python,list,len,num,随机数,print,rounds,从小到大 来源: https://www.cnblogs.com/golden-tea/p/15589072.html