Python选择排序算法 如何做!急求!!
样例输入输出Enteranumber(〈Enter〉toquit):1Enteranumber(〈Enter〉toquit):4Enteranumber(〈Enter〉t...
样例输入输出
Enter a number(〈Enter〉to quit):1
Enter a number(〈Enter〉to quit):4
Enter a number(〈Enter〉to quit):2
Enter a number(〈Enter〉to quit):7
Enter a number(〈Enter〉to quit):6
Enter a number(〈Enter〉to quit):3
Enter a number(〈Enter〉to quit):
before sorted:[1,4,2,7,6,3]
after sorted:[1,2,3,4,6,7] 展开
Enter a number(〈Enter〉to quit):1
Enter a number(〈Enter〉to quit):4
Enter a number(〈Enter〉to quit):2
Enter a number(〈Enter〉to quit):7
Enter a number(〈Enter〉to quit):6
Enter a number(〈Enter〉to quit):3
Enter a number(〈Enter〉to quit):
before sorted:[1,4,2,7,6,3]
after sorted:[1,2,3,4,6,7] 展开
展开全部
#coding: utf-8
#!/usr/bin/python import random
#随机生成0~100之间的数值def get_andomNumber(num):
lists=[]
i=0
while i<num:
lists.append(random.randint(0,100))
i+=1
return lists
# 选择排序def select_sort(lists):
count = len(lists)
for i in range(0, count):
min = i
for j in range(i + 1, count):
if lists[min] > lists[j]:
min = j
temp = lists[min]
lists[min] = lists[i]
lists[i] = temp
return lists
a = get_andomNumber(10)
print("排序之前:%s" %a)
b = select_sort(a)
print("排序之后:%s" %b)
#!/usr/bin/python import random
#随机生成0~100之间的数值def get_andomNumber(num):
lists=[]
i=0
while i<num:
lists.append(random.randint(0,100))
i+=1
return lists
# 选择排序def select_sort(lists):
count = len(lists)
for i in range(0, count):
min = i
for j in range(i + 1, count):
if lists[min] > lists[j]:
min = j
temp = lists[min]
lists[min] = lists[i]
lists[i] = temp
return lists
a = get_andomNumber(10)
print("排序之前:%s" %a)
b = select_sort(a)
print("排序之后:%s" %b)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询