TMP = []defbucket_sort (old): forIinchRange (len (old)): Tmp.append ([]) forIinchOld:tmp[int (i*len (old))]. Append (i) forIinchRange (len (old)):#Tmp[i].sort ()Insert_sort (Tmp[i]) forIinchRange (len (old)):if(Len (tmp[i]) >0):PrintTmp[i]#Test caseOld = [0.3333333333333333, 0.8333333333333334, 0.5, 0.0, 0.3333333333333333, 0.5, 0.0, 0.5]bucket_sort (old) insert_sort (old)PrintOld
The bucket ordering assumes that the input is randomly distributed evenly between [0,1].
Bucket sequencing time complexity O (N)
A[n]: Original array
B[n]: b[i] = a[i] * n
C[n]: c[i] The head pointer in the linked list
Step 1
B[i] = a[i] * n
Step 2
According to the integral part of B[i], A[n] is divided, and the divided A[i] is deposited in the linked list of the corresponding c[i].
Step 3
Sort the list in c[i] separately
Step 4
Merge the sorted c[i], from 0 to N-1
Bucket sort Python Implementation source code