defbubble_sort(A):
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
A = np.array(A)
plt.ion()
x = np.arange(0,len(A))whileTrue:
number =0for i inrange(len(A)-1):if A[i]> A[i+1]:
A[i],A[i+1]= A[i+1], A[i]
number +=1
ax.cla()
ax.bar(x, A)
plt.pause(0.1)if number ==0:breakprint(A)
plt.ioff()
plt.show()
选择排序
defselection_sort(A):
plt.clf()
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
plt.ion()
A = np.array(A)
x = np.arange(len(A))for i inrange((len(A))):
index_min = i
for j inrange(i,len(A)):if A[j]< A[index_min]:
index_min = j
A[i],A[index_min]= A[index_min], A[i]
ax.cla()
ax.bar(x,A)
plt.pause(0.01)print(A)
plt.ioff()
plt.show()
插入排序
definsert_sort(A):
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
plt.ion()
L =[max(A)+1]
A = np.array(A)
ii =0for i in A :
ii +=1for j inrange(len(L)):if i < L[j]:
L.insert(j,i)break
ax.cla()
x = np.arange(len(L)-1)
ax.bar(x,L[:-1])
plt.pause(0.01)
L = L[:-1]
plt.ioff()
plt.show()
希尔排序
defShell_sort(A):
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
plt.ion()
length =int(len(A)/2)whileTrue:for i inrange(length,len(A)):
number = A[i]
g = i - length
while A[g]> number and g >=0:
A[g + length]= A[g]
g -= length
A[g+length]= number
ax.cla()
ax.bar(np.arange(len(A)),A)
plt.pause(0.3)
length =int(length/2)if length <1:breakprint(A)
plt.ioff()
plt.show()
快速排序
defquick_sort(A):iflen(A)>=2:
left, right =[],[]
temp = A[0]
A.remove(temp)for item in A :if item >= temp:
right.append(item)else:
left.append(item)return quick_sort(left)+[temp]+ quick_sort(right)else:return A
归并排序
classMerge_sort(object):def__init__(self,A):
self.A =list(A)print(self.merge(self.A))defmerge(self,lis):iflen(lis)<=1:return lis
cut =len(lis)//2
left = self.merge(lis[:cut])
right = self.merge(lis[cut:])return self.sort(left,right)defsort(self,left,right):
rel,rig =0,0
result =[]while rel <len(left)and rig <len(right):if left[rel]< right[rig]:
result.append(left[rel])
rel +=1else:
result.append(right[rig])
rig +=1
result +=list(left[rel:])
result +=list(right[rig:])return result
堆排序
classHeap_Sort(object):def__init__(self,array):
self.A = array
self.Heap_sort()defSubSort(self,num, index):
larg = index
l = index*2+1
r = index*2+2if l < num and self.A[index]< self.A[l]:
larg = l
if r < num and self.A[larg]< self.A[r]:
larg = r
if larg != index:
self.A[index],self.A[larg]= self.A[larg],self.A[index]
self.SubSort(num, larg)defHeap_sort(self):
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
num =len(self.A)for i inrange(num,-1,-1):
self.SubSort(num, i)for i inrange(num-1,0,-1):
self.A[i],self.A[0]= self.A[0],self.A[i]
self.SubSort(i,0)
ax.cla()
ax.bar(np.arange(len(self.A)),self.A)
plt.pause(0.01)
plt.ioff()
plt.show()print(self.A)
基数排序
defRadix_sort(A):
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
A =list(A)
length =0
lar =max(A)while lar !=0:
length +=1
lar //=10for i inrange(1,4):
per =1for ii inrange(i-1):
per *=10
bucket =[[]for ii inrange(10)]print(per)# print(bucket)for item in A :if i ==1:
num = item %10
bucket[num].append(item)else:
num =(item // per)%10
bucket[num].append(item)
A =[]for j inrange(len(bucket)):for jj in bucket[j]:
A.append(jj)
ax.cla()
ax.bar(np.arange(len(A)),A)
plt.pause(0.3)
plt.ioff()
plt.show()# print(A)