Base sort (radixsort)

Source: Internet
Author: User

Base sort
    • The idea of a cardinal sort algorithm is interesting, and he does not rely on comparative sorting. Instead, they are sorted by allocation. The cardinality sort is also known as bucket ordering. The cardinality sort has the highest bit first (MSD) and the lowest bit first (LSD) two kinds. The following is an example of the lowest bit priority.
Principle

Prepare 10 containers, number 0-9, corresponding to the number 0-9. Containers are ordered (in order of addition)
Then the number of elements to be sorted (for example: digit/10 bit/white bit) is stored in the corresponding container (the same number, such as: 1 in the number 1th barrels),
The order of the elements is removed from container No. No. 0 and then to container number 9th. So the elements inside the container are collected and copied back to the original array , and then the next one begins ...

This assumes that the array elements are all 3-bit numbers. Starting from the single digit, the elements in the array are placed in the corresponding bucket by single digit, and then taken from the bucket in order to the array, this is an array of digits sorted by digit number, and then the same logic processing 10 bits and hundreds. The last array is ordered.

Example code 1

The sample algorithm is not very efficient (the number of bits in the test data is 3 bits and the quantity is 150,000.) ) is much lower than the fast sort, but it's not about the algorithm, but because the example uses the list as a container (bucket), and the catch hits low, resulting in frequent memory accesses.

Class radixsort{PrivateNode[] links =Newnode[Ten];Private bytedigits =3;//number of bits of the element     Public void Sort(int[] arr) {intCount =1; for(intj =0; J < digits; J + +) {//one-cycle processing One             for(inti =0; i < arr.length; i++) {Add (NewNode (Arr[i]), (arr[i]/count)%Ten);            } copy (arr); count*=Ten; }    }Private void Copy(int[] arr) {//Copy elements from the linked list back to the array        intK =0;//Array subscript         for(inti =0; I <Ten; i++) { while(Links[i]! =NULL) {arr[k++] = Links[i].getvalue ();            Links[i] = Links[i].getnext (); }        }    }Private void Add(Node node,intIndex) {if(Links[index] = =NULL){//List null new element in the first placeLinks[index] = node; }Else{//Traverse the list to find the last nodeNode flag = Links[index]; while(Flag.getnext ()! =NULL) {flag = Flag.getnext ();        } flag.setnext (node); }}}class node{Private intValuePrivateNode Next; Public Node(intValue) { This. Next =NULL; This. value = value; } Public int GetValue() {returnValue } Public void SetValue(intValue) { This. value = value; } PublicNodeGetNext() {returnNext } Public void Setnext(Node Next) { This. next = Next; }}


 
改进

使用集合替换链表, 效率提升很大(100x)
 
/* Improved 2017/10/28 * RadixSort1 performance is not good because the list cache hit is too low * Now replace the list with a set * conclusion: the use of linkedlist efficiency and ArrayList, whether it is not related to the cache hit, but the actual Current list efficiency is too low ... * */class radixsort2{private byte digits = number of bits of 3;//element private list[] List = new LIST[10];p ublic RadixSort2 () {SUP ER (); for (int i = 0; i < list.length; i++) {//linkedlist and ArrayList performance are equivalent,  arraylist specified capacity is equal to unspecified. Strange list[i] = new Arrayli St (10000);}}  public void sort (int[] arr) {int count  = 1;for (int j = 0;j < digits; J + +) {//cycles over one bit for (int i = 0;i < arr.length; i++) {//add (new Node (Arr[i]), (arr[i]/count)%), list[(arr[i]/count)% 10].add (Arr[i]);} Copy (arr); count*=10;}} private void Copy (int[] arr) {//The elements in the list are copied back to the array int k = 0;//array subscript for (int i = 0; i < list.length; i++) {for (Object o:list[i ]) {arr[k++] = (Integer) o;} List[i].clear ();}}}

  

 

Base sort (radixsort)

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.