冒泡排序
n=int(input()) #5
a=list(map(int,input().split(","))) #7,6,5,4,3
for i in range(0,n-1): #循环n-1次
for j in range(0,n-i-1): #循环n-i次,依次找第二大,第三大的等等
if a[j]>a[j+1]:
a[j],a[j+1]=a[j+1],a[j] #前大于后的交换位置
print(','.join(map(str,a))) #3,4,5,6,7
print(*a) #3 4 5 6
选择排序
n=int(input())
a=list(map(int,input().split()))
for i in range(0,n-1):
min_value=a[i]
min_index=i
for j in range(i,n):
if a[j]<min_value:
min_value=a[j]
min_index=j
a[i],a[min_index]=a[min_index],a[i]
print(*a)
插入排序
n=int(input())
a=list(map(int,input().split()))
for i in range(1,n):
value=a[i]
index=0
for j in range(i-1,-1,-1):
if a[j]>value:
a[j+1]=a[j]
else:
index=j+1
break
a[index]=value
print(*a)
快速排序
def partition(a,left,right):
index=left+1
for i in range(left+1,right+1):
if a[i]<=a[left]:
a[index],a[i]=a[i],a[index]
index+=1
a[left],a[index-1]=a[index-1],a[left]
return index-1
def quicksort(a,left,right):
if left<right:
mid=partition(a,left,right)
quicksort(a,left,mid-1)
quicksort(a,mid+1,right)
n=int(input())
a=list(map(int,input().split()))
quicksort(a,0,n-1)
print(*a)
归并排序
n=int(input())
a=list(map(int,input().split()))
def merge(A,B):
result=[]
while len(A)!=0 and len(B)!=0:
if A[0]<=B[0]:
result.append(A.pop(0))
else:
result.append(B.pop(0))
result.extend(A)
result.extend(B)
return result
def mergesort(A):
if len(A)<2:
return A
mid=len(A)//2
left=mergesort(A[:mid])
right=mergesort(A[mid:])
return merge(left,right)
print(' '.join(map(str,mergesort(a))))
桶排序
from itertools import chain
def Bucket_sort(a,bucketcount):
minvalue,maxvalue=min(a),max(a)
bucketsize=(maxvalue-minvalue+1)//bucketcount
res=[[] for i in range(bucketcount+1)]
for x in a:
index=(x-minvalue)//bucketsize
res[index].append(x)
for resx in res:
resx.sort()
return list(chain(*res))
n=int(input())
a=list(map(int,input().split()))
a=Bucket_sort(a,min(n,10000))
print(*a)
标签:index,map,python,list,range,int,算法,input,排序
From: https://blog.csdn.net/qq_68268403/article/details/137468336