This is a creation in Article, where the information may have evolved or changed. > Because before the Golang Chinese community did not find radix-sort embarrassed me, I gave the following code, if not enough please indicate "Goimport" StrConv "func radixsort (arr []int) []int{if Len (arr) <2{fmt. PRINTLN ("NO need to SORT") return Arr}maxl:=maxlen (arr) return Radixcore (ARR,0,MAXL)}func radixcore (arr []int,digit, MAXL int) []int{//core sorting mechanism time complexity O (d (r+n)) if Digit>=maxl{return arr//Sort stable}radix:=10count:=make ([]int,radix) bucket:= Make ([]int,len (arr)] for I:=0;i<len (arr); I++{count[getdigit (arr[i],digit)]++}for i:=1;i<radix;i++{count[i]+ =count[i-1]}for I:=len (arr) -1;i>=0;i--{d:=getdigit (arr[i],digit) Bucket[count[d]-1]=arr[i]count[d]--}return Radixcore (BUCKET,DIGIT+1,MAXL)}func getdigit (x,d int) int{//Get number on a bit a:=[]int{1,10,100,1000,10000,100000,1000000} Return (X/a[d])%10}func maxlen (arr []int) int{//Get maximum number of bits var maxl,curl intfor I:=0;i<len (arr); I++{curl=len (StrConv. Itoa (Arr[i])) if Curl>maxl{maxl=curl}}return Maxl} "" This is the LSD method (sort stable), that is, starting from the lowest bit to compare oh, there is a comparison from the highest bit of the method MSD (unstable ordering). 266 Reads
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.