Skip to content

Commit 2595d06

Browse files
author
miaozaiye
committed
add bubble.py
1 parent 81eb1d0 commit 2595d06

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

.heap_sort.py.swp

12 KB
Binary file not shown.

bubble.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
for index in range(l2):
17+
temp = List[index]
18+
((List[index] = List[index+1]) and (List[index+1] = temp)) if List[index] > List[index+1] else pass
19+
l2 = l2-1
20+
21+
22+
return List
23+
24+
print (bubble(List))

0 commit comments

Comments
 (0)