python算法实践3 冒泡排序
#冒泡排序
def BubbleSort(mylist):
n = len(mylist)
i = 0
j = 0
bExchange = False
for i in range(1, n):
bExchange = False
j = n - 1
while j >= i:
if mylist[j] < mylist[j - 1]:
tmp = mylist[j]
mylist[j] = mylist[j - 1]
mylist[j - 1] = tmp
bExchange = True
j = j - 1
if not bExchange:
break
mylist0 = [11, 10, 4, 2, 1, 55, 99, 102]
BubbleSort(mylist0)
print(mylist0)
相关文档:
最近,想在我的YouMoney(http://code.google.com/p/youmoney/)里面增加提取用户操作系统版本信息。比如windows用户,可能要返回Windows XP ,或者Windows 2003, 苹果用户应该返回Mac OS X 10.5.8。用了很多办法,包括在mac系统里调用系统命令,取环境变量,等等。最后无意发现,原来python里里面有个pl ......
python字典排序
1、
准备知识:
在python里,字典dictionary是内置的数据类型,是个无序的存储结构,每一元素是key-value对:
如:dict = {‘username’:‘password’,‘database’:‘master’},其中‘username’和& ......
#shell排序
def ShellPass(mylist, d):
size = len(mylist)
i = d
while i < size:
if mylist[i] < mylist[i - d]:
tmp = mylist[i]
j = i - d
mylist[j + d] = mylist[j]
j = j - d
while j >= 0 and mylist[j] > ......