- 冒泡排序
def BubbleSort(x):
for i in range(0,len(x)-1):
for j in range(i+1,len(x)):
if x[i]<x[j]:
temp=x[i]
x[i]=x[j]
x[j]=temp
return x
import random
import numpy as np
x=np.random.randint(1,20,size=10)
BubbleSort(x)
- 快排
def qsort(arr):
if len(arr)==1:
return arr
else:
pivot = arr[0]
return qsort([x for x in arr[1:] if x < pivot]) + [pivot] + qsort([x for x in arr[1:] if x >= pivot])