Skip to content

Commit 00fa350

Browse files
authored
Merge pull request #2 from miaozaiye/Jane
Jane
2 parents 46e7629 + 8fbc933 commit 00fa350

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

bubble.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# 这是冒泡排序算法的python 实现 by [email protected]
2+
# bubble 的原理,是将相邻两个数字做对比,将大的排在后面;这样每轮排下来,最后一个总是最大的;下一轮,则只用排前 n-1 个,直到 n==1。
3+
'''
4+
>>>bubble.py 3,6,7,2,15
5+
6+
2,3,6,7,15
7+
8+
'''
9+
10+
List = [1,2,3,4,5]
11+
def bubble(List):
12+
l = len(List)
13+
l2 = l
14+
15+
for count in range(l):
16+
print (l,l2)
17+
for index in range(l2):
18+
print (l,l2,index)
19+
if index == l2-1:
20+
pass
21+
else:
22+
temp = List[index]
23+
if List[index] > List[index + 1]:
24+
print (l,l2,index)
25+
List[index] = List[index+1]
26+
List[index+1] = temp
27+
l2 = l2-1
28+
29+
30+
return List
31+
32+
print (bubble(List))

0 commit comments

Comments
 (0)