Sorting algorithm Base Order

Source: Internet
Author: User

1. Basic Ideas

Unify all the values to be compared (positive integers) to the same digit length, with a short number of digits preceded by 0. Then, start with the lowest bit and order one at a time. Thus, from the lowest bit to the highest order, the sequence becomes an ordered series.

2. code example
 Packagesort;Importjava.util.ArrayList;Importjava.util.List;Importorg.junit.Test;/*** Base Sort*/ Public classRadixsort {@Test Public voidTestsort () {intA[] = {49, 38, 65, 97, 76, 13, 27, 49, 78, 53, 51 }; Sort (A,2); }     Public voidSortint[] arr) {        intlen=arr.length; //first, the number of sorts is determined;        intmax = Arr[0];  for(inti = 1; i < Len; i++) {            if(Arr[i] >max) {Max=Arr[i]; }        }        intTime = 0; //determine the number of digits;         while(Max > 0) {Max/= 10; time++; }        //set up 10 queues;list<arraylist> queue =NewArraylist<arraylist>();  for(inti = 0; I < 10; i++) {ArrayList<Integer> queue1 =NewArraylist<integer>();        Queue.add (queue1); }        //time allocation and collection;         for(inti = 0; I < time; i++) {            //assigning array elements;             for(intj = 0; J < Len; J + +) {                //get the number of time+1 digits;                intx = arr[j]% (int) Math.pow (i + 1))                        / (int) Math.pow (10, i); ArrayList<Integer> queue2 =queue.get (x);                Queue2.add (Arr[j]);            Queue.set (x, queue2); }            intCount = 0;//element counter; //collect queue elements;             for(intk = 0; K < 10; k++) {                 while(Queue.get (k). Size () > 0) {ArrayList<Integer> Queue3 =Queue.get (k); Arr[count]= Queue3.get (0); Queue3.remove (0); Count++; }            }        }         for(inti = 0; i < Len; i++) {System.out.print (Arr[i]+" "); }                }         Public voidSortint[] arr,intD//d indicates the maximum number of digits    {        intlen=arr.length; intK = 0; intn = 1; intm = 1;//Control key values are sorted by which one        int[][]temp =New int[10] [Len];//the first dimension of the array represents the possible remainder 0-9        int[]order =New int[10];//Array Orderp[i] is used to indicate the number of digits that the bit is I         while(M <=d) { for(inti = 0; i < Len; i++)            {                intLSD = ((Arr[i]/n)% 10); TEMP[LSD][ORDER[LSD]]=Arr[i]; ORDER[LSD]++; }             for(inti = 0; I < 10; i++)            {                if(order[i]! = 0)                     for(intj = 0; J < Order[i]; J + +) {Arr[k]=Temp[i][j]; K++; } Order[i]= 0; } N*= 10; K= 0; M++; }         for(inti = 0; i < Len; i++) {System.out.print (Arr[i]+" "); }    }}
3. Efficiency analysis

Sorting algorithm Base Order

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.