[Python-算法]python实现冒泡,插入,选择排序

发布时间:2017-09-02 15:49:26
[Python-算法]python实现冒泡,插入,选择排序

从新开始学习数据结构和算法,站群系统,专题系统,从前没有系统学过。

[python]
# -*- coding: cp936 -*-
#python插入排序
def insertSort(a):
for i in range(len(a)-1):
#print a,i
for j in range(i+1,len(a)):
if a[i]>a[j]:
temp = a[i]
a[i] = a[j]
a[j] = temp
return a


#Python的冒泡排序
def bubbleSort(alist):
for passnum in range(len(alist)-1,0,-1):
#print alist,passnum
for i in range(passnum):
if alist[i]>alist[i+1]:
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp
return alist


#Python的选择排序
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):
maxone = 0
for j in range(1,i+1):
if alist[j]>alist[maxone]:
maxone = j
temp = alist[i]
alist[i] = alist[maxone]
alist[maxone] = temp
return alist


alist = [54,26,93,17,77,31,44,55,20]
#print bubbleSort(alist)
alist = [54,26,93,17,77,31,44,55,20]
print selectionSort(alist)

# -*- coding: cp936 -*-
#python插入排序
def insertSort(a):
for i in range(len(a)-1):
#print a,i
for j in range(i+1,len(a)):
if a[i]>a[j]:
temp = a[i]
a[i] = a[j]
a[j] = temp
return a


#Python的冒泡排序
def bubbleSort(alist):
for passnum in range(len(alist)-1,0,-1):
#print alist,passnum
for i in range(passnum):
if alist[i]>alist[i+1]:
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp
return alist


#Python的选择排序
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):
maxone = 0
for j in range(1,i+1):
if alist[j]>alist[maxone]:
maxone = j
temp = alist[i]
alist[i] = alist[maxone]
alist[maxone] = temp
return alist


alist = [54,26,93,17,77,31,44,55,20]
#print bubbleSort(alist)
alist = [54,26,93,17,77,31,44,55,20]
print selectionSort(alist)


企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉网站设计 http://www.feimao666.com