Read a reading tonight, ran will step, idle to look at the data structure of strict grandmother, found that the cardinal sort is very interesting, with a multi-key idea, in the case of less base can achieve better results.
The explanation in the book is easy to understand (but I can't understand the code of my grandmother), I read it all at once. Open your computer and start practicing now.
Learning Links: Fastest and simplest sort-bucket sequencing (super-cute comics, very easy to understand), cardinality sorting, sorting algorithm series: cardinal sort
Today, the compilation is very cool, debugging is also very cool. Only three times the debug, there is no worm. A worm is more mentally retarded, I am dizzy. There is also a bug in the "position to the tail of the list" This operation has a logical blind spot, a bug is in the "Construction cardinality linked list" is not the pioneer node to wipe the butt operation, resulting in post-processing dead loop.
The only regret is that this test code can only be used to sort the test data without negative numbers, and if it contains negative numbers, the cardinality represented by the radix array is certainly not 0~9, but, -9~9, a lot of things to change.
Java code:
1 Public classMain {2 3 Public Static voidMain (string[] args) {4 int[]nums={3,2,4,6,7,1,3,11,100,1000,1,20};5Radixsort sort=NewRadixsort (nums);6 System.out.print (sort);7 }8 }9 Ten classradixsort{ One int[] Sortans; A classlinkednums{ - intNum=0; -Linkednums next=NULL; the } -Linkednums first=Newlinkednums (); -Linkednums radix[]=NewLINKEDNUMS[10];//0~9 - PublicString toString () { + inti; -String str=NewString (""); + for(i=0;i<sortans.length;i++) str+=string.valueof (Sortans[i]) + ""; Astr+= "\ n"; at returnstr; - } - Private voidFormlinkednums (int[] nums) { - inti; - intlen=nums.length; -Linkednums point=First ; inPoint.num=nums[0]; - for(i=1;i<len;i++){ toLinkednums node=Newlinkednums (); +point.next=node; -Point=Point.next; thepoint.num=Nums[i]; * } $ }Panax Notoginseng Private intGetinnum (intNumintrank) { - return the(Num% (int) Math.pow (10.0, (DoubleRank)))//operation to get the number currently taken out +/ A((int) Math.pow (10.0, (Double) (rank-1))); the } + Private intGetmaxleninarr (int[] nums) { - inti; $ intMax=0; $ for(i=0;i<nums.length;i++){ - intlen=string.valueof (Nums[i]). Length (); - if(Len>max) max=Len; the } - returnMax;Wuyi } theRadixsort (int[] nums) { - inti,j; Wu intlen=nums.length; - for(i=0;i<=9;i++) radix[i]=NULL;//Initializing the Radix About formlinkednums (nums); $ intrank=1;//The operation of X%10 is performed first, then (x%100)/10, and then (x%1000)/100. - intcirnum=Getmaxleninarr (nums); - for(j=0;j<cirnum;j++){ -Linkednums point=First ; A while(point!=NULL){ + intnum=Getinnum (Point.num,rank); the if(radix[num]==NULL){//cardinality array does not have a tick chain -radix[num]=Point ; $}Else{ theLinkednums local=Radix[num]; the while(local.next!=NULL) Local=local.next;//"1" theLocal.next=point;//The hook chain here does not disrupt the operation of the program. Because the local subscript is smaller than point. There is no impact on the subsequent relationship with local destruction. the } -Linkednums Pre=point;//"2" inPoint=Point.next; thepre.next=NULL;//"2" the } About //The cardinality array is chained. Re-construct the linked list. the //first assigns a value. the Booleanislinkedfirst=false; thePoint=NULL; + for(i=0;i<=9;i++){ - if(radix[i]!=NULL){ theLinkednums local=Radix[i];Bayi if(!Islinkedfirst) { thefirst=Local; theislinkedfirst=true; -Point=First ; -Local=Local.next; the } the while(local!=NULL){ thepoint.next=Local; thePoint=Point.next; -Local=Local.next; the } the } the }94 for(i=0;i<=9;i++) radix[i]=NULL;//Initializing the Radix therank++;//Increment of order the } the //writes a linked list to an array of results. 98sortans=New int[Len]; AboutI=0; - while(first!=NULL){101sortans[i++]=First.num;102first=First.next;103 }104 } the}
Logical Blind Zone Summary:
"1": Line 67. Local.next!=null is written as local!=null, resulting in a null pointer error.
"2": Line 70, 72. There is no action to wipe the butt of the precursor node.
Base sort Radixsort