代码1:
def ssort(v):#v is the list to be sorted j = 0 #j is the current ordered position, starting with the first one in the list while j != len(v): #this is the replacing that ends when it reaches the end of the list for i in range(j, len(v)): #here it replaces the minor value that it finds with j position if v[i] < v[j]: #but it does it for every value minor than position j v[j],v[i] = v[i],v[j] j = j+1 #and here's the addiction that limits the verification to only the next values return v
代码2:
def selection_sort(list): l=list[:] # create a copy of the list sorted=[] # this new list will hold the results while len(l): # while there are elements to sort... lowest=l[0] # create a variable to identify lowest for x in l: # and check every item in the list... if xl[j]: # to compare two values which store in num and list index=j # to store the small value of the loop j in index num=l[j] # to store small charecter are value in num tem=l[i] # to swap the list take the temparary list stor list vlaues l[i]=l[index] # to take first value as another l[index]=tem print after the swping the list by selection sort is,l
希望本文所述对大家的python程序设计有所帮助。