1. Bubble sort, adjacent position compare size, will be larger (or smaller) swap locationdef Maopao (a):
For i in range (0,len (a)):
For J in Range (0,len (a)-i-1):
if a[j]>a[j+1]:
temp = a[j+1]
a[j+1] = a[j]
A[j] = temp
#print (a)
#print (a)
print (a)
2. Select Sort, traverse to select a minimum number to exchange with the first number of the current loopdef Xuanze (a):
For i in range (0,len (a)):
k=i
temp = a[i]
For J in Range (I,len (a)):
if a[j]<temp:
temp = a[j]
k = J
A[k] = a[i]
A[i] = temp
print (a)3. Quick sort: The first element of the sub-paragraph as the median, first from right-to-left traversal, such as over-high-1, if the value is smaller than the median, put this value to low there. then walk from left to right, and if the left side is larger than the median, put him there. When Low>=high, assigns the value of the median value to low 1). The following is a reference to the practice in the public number:a =[7,1,3,2,6,54,4,4,5,8,12,34]
def sort (a,low,high):
While low < high:
temp = A[low]
While low < high and A[high]>=temp:
High = High-1
A[low]=a[high]
While Low
Low = Low+1
A[high]=a[low]
a[low]=temp
return Low
def quicksort (a,low,high):
if Low
middle = sort (A,low,high)
quicksort (A,low,middle)
quicksort (A,middle+1,high)
print (a)
Sort (A,0,len (a)-1)
quicksort (A,0,len (a)-1)
print (a) 2). The following is a reference to the online approach:#在做快速排序时一直各种问题, because the Ark is not considered clearly, the value of low has been assigned to a value of 0,
#实际上应该是不固定的low值, he had a variable cycle. a =[7,1,3,2,6,54,4,4,5,8,12,34]
def sort (a,low,high):
While low < high:
temp = A[low]
While low < high and A[high]>=temp:
High = High-1
While Low
A[low]=a[high]
Low =low+1
A[high]=a[low]
a[low]=temp
return Low
def quicksort (a,low,high):
if Low
middle = sort (A,low,high)
quicksort (A,low,middle)
quicksort (A,middle+1,high)
print (a)
Sort (A,0,len (a)-1)
quicksort (A,0,len (a)-1)
print (a) 4 Insert Sort: traverse from left to right, select numeric value, walk right to left from the left side of the value, select the first value to the right of the number that is smaller than him, and the other values are assigned in turn backwards#插入排序
a =[7,1,3,2,6,54,4,4,5,8,12,34]
For i in range (0,len (a)-1):
Temp=a[i+1]
j=i+1
While j>=0 and temp<a[j-1]:
j=j-1
print (j)
if J>=-1:
k= i+1
While k>=j:
A[k]=a[k-1]
k=k-1
print (a)
a[j]=temp
print (a) Insert Sort Method 2, use the list of A.insert and clear a[2:3]=[], so that you can use less a loopa =[7,1,3,2,6,54,4,4,5,8,12,34]
For i in range (1,len (a)-1):
Temp=a[i]
j=i-1
While j>=0 and Temp<=a[j]:
Print (temp)
j=j-1
if J >=-1:
a[i:i+1]=[]
A.insert (j+1,temp)
print (a)
print (a)
Common sorting methods implemented by Python