基數排序
按照個位數,十位數,百位數.... 進行排序
Radix-sort(A,d)for i = 1 to d use a stable sort to sort array A on digit i
#python#帶有衛星數據的穩定排序def partition(A,B,p,r): x = A[r] i = p - 1 for j in range(p,r): if A[j] <= x: i = i + 1 A[i],A[j] = A[j],A[i] B[i],B[j] = B[j],B[i] A[i+1],A[r] = A[r],A[i+1] B[i+1],B[r] = B[r],B[i+1] return i+1def quick_sort(A,B,p,r): if p < r: q = partition(A,B,p,r) quick_sort(A,B,p,q-1) quick_sort(A,B,q+1,r)def moon_sort(A,d,p,r): B = [] for i in range(0,len(A)): B.append(A[i]%10**d/10**(d-1)) quick_sort(B,A,p,r)def radix_sort(A,d,p,r): for i in range(1,d+1): moon_sort(A,i,p,r)A = [191,123,234,765,489,520]print start A:,Aradix_sort(A,3,0,len(A)-1)print end A:,A
推薦閱讀:
TAG:演算法 |