We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 46e7629 + 8fbc933 commit 00fa350Copy full SHA for 00fa350
bubble.py
@@ -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
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