Eight, the base sort
Basic idea: the Cardinal sort (radix sort) is "assigned sort" (distribution sort), also known as "bucket method" (bucket sort) or bin sort, as the name implies, it is part of the information through the key value, The elements to be sorted are assigned to some "buckets", in order to achieve the order, the cardinal sort method is a sort of stability, its time complexity is O (Nlog (r) m), where R is the cardinality taken, and M is the number of heaps, at some point, the cardinality of the sorting method is more efficient than other stability ranking method.
Algorithm implementation:
#Coding:utf-8#!/usr/bin/pythonImportRandomImportMath#randomly generate a value between 0~100defget_andomnumber (num): Lists=[] I=0 whilei<num:lists.append (random.randint (0,100)) I+=1returnlists#The head needs to be imported into mathdefRadix_sort (lists, radix=10): K=Int (Math.ceil (Math.log (max (lists), radix )) bucket= [[] forIinchrange (radix)] forIinchRange (1, k+1): forJinchLists:bucket[int (J/(radix** (i-1))% (radix**i)]. Append (j)dellists[:] forZinchbucket:lists+=Zdelz[:]returnListsa= Get_andomnumber (10)Print("before sorting:%s"%a) b=Radix_sort (a)Print("after sorting:%s"%b
Nine sorting algorithms you need to know the cardinality of the "Python implementation"