python——冒泡排序练习

冒泡排序:就是将列表里的混乱的整形或其他进行有效的排序。
test1:
num
=list('2574') n=len(num) print(num) for i in range(0,n): for j in range(i+1,n): if num[i]>num[j]: #a>b c=num[j] #c=b num[j]=num[i] #b=a num[i]=c #a=c print(num) print('排序最总的结果为:'+ str(num)) test2 num=list('6457') n=len(num) print(num) for i in range(0,n): for j in range(i+1,n): if num[i]>num[j]: num[i],num[j]=num[j],num[i] print(num) print('排序最总的结果为:'+ str(num)) test3
import random num=[] x=int(input('请问需要多少个随机数')) for i in range(x): num.append(random.randint(1,100)) n=len(num) print(num) for i in range(0,n): for j in range(i,n): if num[i]>num[j]: num[i],num[j]=num[j],num[i] print(num)

 

posted @ 2018-03-28 17:37  Leonardo-li  阅读(349)  评论(0编辑  收藏  举报