Four common sorting methods implemented in Python

Time：2021-12-1

1. Bubble sort, compare the size of adjacent positions, and exchange the larger (or smaller) positions

def 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 sorting, traverse and select a minimum number to exchange with the first number of the current cycle

def 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: take the first element of the sub segment as the median value, and traverse from right to left first. If it is higher than the median value by high-1, if it is lower than the median value, put the value at low.

Then start traversing from left to right. If the left is larger than the median, put it there at high. When low > = high, assign the value of the median to low

(1. below is a reference to the official account 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<high and a[low]<temp:
low = low+1
a[high]=a[low]
a[low]=temp
return low
def quicksort(a,low,high):
if low<high:
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 practice:

There have been various problems in quick sorting because the floor cabinet has not been considered clearly. It has always assigned the value of low to 0. In fact, it should be an indefinite low value, and each sub cycle is indefinite.

'''
No one answers any questions? Xiaobian created a python learning exchange group: 531509025
Look for like-minded friends to help each other. There are also good video learning tutorials and PDF e-books in the group!
'''
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<high and a[high]<temp:
a[low]=a[high]
low =low+1
a[high]=a[low]
a[low]=temp
return low
def quicksort(a,low,high):
if low<high:
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 the value in turn, traverse from the left side of the value from right to left, select the right side of the first smaller value, insert the value, and assign other values backward in turn

#Insert sort
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 sorting method 2 uses A. insert (1,2) of the list and a [2:3] = [], which can save one cycle

a =[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)

This is the end of this article about the four methods of Python sorting. For more information about Python sorting methods, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!

Sort out recently used Mongo query statements

background Recently, I have made several rule logic. Mongo query is often used, which is to query transaction information, run established rules to screen out trading merchants, use aggregation pipeline for statistics and take out simple processed data, and use SQL instead of business code logic judgment. method Mongodb aggregation usageaggregate, the aggregation pipeline adopts […]