The python implementation described in this article is bubbling, inserting, selecting a simple instance of sort for Python beginners to learn from the basics of data structures and algorithms, examples are simple to understand, the code is as follows:
#-*-coding:cp936-*-#python插入排序def Insertsort (a): for I in range (Len (a)-1): #print a,i for J in range (i+1,l En (a)): if A[I]>A[J]: temp = a[i] a[i] = a[j] a[j] = temp return A#python bubble sort 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 selection Sort 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 alistalist = [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 try to test the examples of this article, I believe there will be a new harvest.