#Python冒泡排序(以升序为例):
s = [0.1,3,9,5,2,6]
for i in range(0,len(s)-1):
for j in range(i+1,len(s)):
if s[i]>s[j] :
t = s[i]
s[i] = s[j]
s[j] = t
print(s)
#Python选择排序(以升序为例):
s = [0.1,3,9,5,2,6,3.2]
for i in range(0,len(s)-1):
min = i
for j in range(i+1,len(s)):
if s[min]>s[j] :
min = j
if s[i] != s[min] :
t = s[min]
s[min] = s[i]
s[i] = t
print(s)
扩充:Python中的枚举
使用 enumerate() 函数,将列表或元组中的元素及其对应的下标值打印出来。
s = [0.1,3,9,5,2,6]
for index,value in enumerate(s):
print(index,value)
结果为:
0 0.1
1 3
2 9
3 5
4 2
5 6
若为 for i in enumerate(s):
print(i)
结果为:(0, 0.1)
(1, 3)
(2, 9)
(3, 5)
(4, 2)
(5, 6)