Sorting algorithm Series--Cardinal sort

Source: Internet
Author: User
Tags pow

Cardinality sorting differs from the other seven sorting algorithms, which are based on an allocation method rather than a comparison. The cardinality sort belongs to the "distributive sort" (distribution sort), the cardinal Sort method is also called "bucket method" (bucket sort) or bin sort, as the name implies, it is through the key value of the part of the information, will be ordered by the elements assigned to some "bucket", in order to achieve the role of sorting. It is inspired by the queue, and its most unique place is to take advantage of the poverty of numbers (Arabic numerals only 0 to 9 of 10).
Basic Ideas
We consider the process of cardinal ordering of integers, and for integers we can divide them into bits, 10 bits, hundred 、。。。 The base sort is to sort the elements in bits first, then sort the 10 bits ... The final sort is done by the highest bit to complete the sorting. Logic can be understood as, for example, under normal circumstances we sort two integers, we first compared to 10 bits and then the bit, now we reverse the first sort of bits, and then sort 10 bits, if the 10 bit the same previous step we have been ranked in order, so it is orderly. In fact, the cardinality sort also has another way of MSD, that is, starting from the high.
Implementation Essentials
First we need a bucket that can put down all the numbers, but only 10 of the Arabic numerals, so we need only 10 buckets, but when we put all the elements into a bucket, there is a case where multiple elements are put into a bucket, This time we need to use the list to solve (there are two-dimensional arrays, but the space needs to n^2, when the ordering element is a bit too much), and in order to facilitate the traversal of the bucket elements and add elements, we let the bucket contains two pointers, One points to the first element in the bucket (head), the other to the last element (tail), and each element in the bucket is a node,node that contains a value (val) in a sort sequence and a pointer to the next element (next).
With the bucket, the next step is to put all the values from the single digit to the bucket, and then in order to put back the original array, there is a place to note, here is how to loop to the top of all elements in the array to terminate the loop, here are two solutions:
(1) First iterate through the array, find the maximum value, determine the highest bit
(2) Keep looping until the specified number of digits for all elements is 0
The first method requires an O (n) comparison, and the second time you need to calculate the value of each value in the specified number of digits, and you need to put it in the bucket. My implementation requires the first way.
Basically you can achieve a base order here, let's see how to implement it.
Java Implementation

 PackageCom.vicky.sort;ImportJava.util.Random;/** * Base Sort * * @author Vicky * */ Public  class radixsort {    Private Static Final intRADIX =Ten; Public Static void Sort(integer[] data) {LongStart = System.nanotime ();if(NULL= = data) {Throw NewNullPointerException ("Data"); }if(Data.length = =1) {return; }//Iterate over, find the maximum, determine the maximum number of digits        intmax = Integer.min_value;inti =0; for(; i < data.length; i++) {if(Data[i] > Max)            {max = data[i]; }        }//Calculate the maximum number of digits        intMaxdigit = string.valueof (max). Length (); bucket[] Temp =NewBucket[radix];//buckets for storing individual bits         for(intnum =0; Num < maxdigit; num++) {//Put the value of the specified number of bits per element into the corresponding bucket             for(i =0; i < data.length; i++) {intval = data[i]/POW (RADIX, num); Node node =NewNode (Data[i),NULL); Bucket tmp = temp[val% RADIX];if(NULL= = tmp) {temp[val% RADIX] =NewBucket (node, node); }Else{Tmp.gettail (). Setnext (node);                Tmp.settail (node); }            }//The elements saved in temp are updated sequentially into the original array            intindex =0; for(i =0; i < temp.length; i++) {if(NULL= = Temp[i]) {Continue;                } node node = Temp[i].gethead (); data[index++] = Node.getvalue (); while(NULL! = Node.getnext ()) {data[index++] = Node.getnext (). GetValue ();                node = Node.getnext (); }//Reset Temp by the wayTemp[i] =NULL; }} System.out.println ("Use time:"+ (System.nanotime ()-start)/1000000); }/** * Calculates the value of the specified power of the specified cardinality * * @param digit * cardinality, such as * @param num * Power value, starting from 0, 10^0=1 * @return * /    Private Static int POW(intDigitintNUM) {intval =1; for(inti =0; i < num;        i++) {val *= digit; }returnVal }/** * <p> * buckets for storing arrays * </p> * * @author Vicky * @date 2015-8-21 */    Private Static  class Bucket {        PrivateNode Head;PrivateNode tail; Public Buckets(Node head, node tail) {Super(); This. Head = head; This. tail = tail; } PublicNodeGetHead() {returnHead } PublicNodeGetTail() {returnTail } Public void Settail(Node tail) { This. tail = tail; }    }/** * <p> * node for resolving multiple element problems in buckets * </p> * * @author Vicky * @date 2015-8-21 * *    Private Static  class Node {        Private intValuePrivateNode Next; Public Node(intValue, Node next) {Super(); This. value = value; This. next = Next; } Public int GetValue() {returnValue } PublicNodeGetNext() {returnNext } Public void Setnext(Node Next) { This. next = Next; }    } Public Static void Main(string[] args) {Random ran =NewRandom (); integer[] data =Newinteger[10000000]; integer[] Data2 =NewInteger[data.length]; integer[] Data3 =NewInteger[data.length]; for(inti =0; i < data.length; i++) {Data[i] = Ran.nextint (100000);            Data2[i] = Data[i];        Data3[i] = Data[i];        } mergesort.sort (data);        Quicksort.sort (DATA2); Radixsort.sort (DATA3);//Sortutils.printarray (data);        //Sortutils.printarray (DATA2);        //Sortutils.printarray (DATA3);}}

Efficiency Analysis :
(1) Complexity of time
O (KN)

The time complexity of the radix sort is O (k n), where n is the number of ordered elements, and K is the number of digits. Note that this is not to say that the complexity of the time must be better than O (n log (n)), because the size of k is generally affected by N. To sort n different integers for example, assuming that these integers are based on B, so that each digit has a B different number, the K must be no less than LOGB (n). Because there are b different numbers, you need a different bucket of B, and in each round the comparison requires an average n log2 (B) comparison to put the whole number into the appropriate bucket, so there are:
K greater than or equal to LOGB (n)
Each round (average) requires n log2 (B) comparison
So, the average time t for the cardinality sort is:
T≥LOGB (n) n log2 (b) = log2 (n) logb (2) n log2 (b) = log2 (n) n logb (2) log2 (b) = n log2 (n)
So, similar to the comparison sort, the number of comparisons required for cardinal ordering: T≥n log2 (n). The time complexity is Ω (n log2 (n)) =ω (n log n).

(2) Complexity of space
O (Radix+n)
Radix a bucket, and N node.
(3) Stability
Stability
Suppose a group of elements: (a), 23,12 (b), 34, 15, first we get bucket (1) in single-digit buckets (a), (b), after extraction or (a), (b) So the order does not change.

Although the cardinality sort is very efficient to look at, it is not as efficient as the quick sort and merge sort, but it is certainly higher than other O (n^2) sorting algorithms. Because the cardinality sort requires an action link, the operation of the list is certainly not as efficient as the array. But the cardinality sort can be used for non-numeric sorting, such as strings. For a set of strings to be sorted, other sorting algorithms cannot be applied, but you can use the cardinal sort, by the string each character divides into the bucket, then sorts, but must first determine how many different characters, if contains all characters, then cannot play. Here is a reference to the applicable scope of the cardinality sort described in an article.

The cardinality is sorted from low to high, so that after the last count is sorted, the array is ordered. The principle is that for the data to be sorted, the overall weight is unknown, first by the weight of a small factor, and then weighted by a significant factor. For example, compare time, sort by day, sort by month, sort by year, order only three times. But it's not so easy to sort the highs first. The cardinal sort originates from the old-fashioned piercing machine, where the sequencer sees only one column at a time, and many of the textbook's cardinality are sorted numerically, and the values are known, unlike old-fashioned puncher. It's boring and asking for trouble to sort the values by bits. The goal of the algorithm is to find the best solution to the problem, rather than making the simple thing more complicated. The cardinality sort is more suitable for sorting data that is unknown to these overall weights such as time, string, and so on. This is the idea of a cardinal sort, such as a string, which can be troublesome if it is from a high (first position) to the back row. In turn, it is very easy to sort the lower (last) bit with less influence and sort out the low-order weight factor. The idea of a cardinal order can be reflected. Or all the values are stored as strings, just like a punch hole, where only one column can be sorted at a time. At this time the cardinal sort also applies, for example: to {"193"; 229 ";" 233 ";" 215 "} to sort.

Reference article :
Java cardinality ordering
Summary of the Java sorting Algorithm (eight): Cardinal sort

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Sorting algorithm Series--Cardinal sort

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.