The Python implementation described in this article bubbles, inserts, and selects simple instances of sorting that are more suitable for Python beginners to learn from the basics of data structures and algorithms, the examples are simple and understandable, the specific code is as follows:
#-*-coding:cp936-*-
#python插入排序
def insertsort (a): for
I in range (Len (a)-1):
#print a,i to
J in R Ange (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 to
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)
Interested friends can test the example of this article, I believe there will be a new harvest.