Before we say the Cardinal sort, let's first say the bucket sort:
The basic idea: is to divide the array into a finite number of buckets. Each bucket is sorted separately (it is possible to use a different sorting algorithm or to sort by using the bucket sort recursively). Bucket sequencing is an inductive result of pigeon nest sorting. When the values in the array to be sorted are evenly distributed, the bucket sort uses linear time (Θ (n)). But the bucket sort is not a comparison sort, and he is not affected by the O (n log n) lower bound.
Simply put, the data is grouped, placed in a bucket, and then the inside of each bucket is sorted.
For example, to sort n integers in the [1..1000] range of size A[1..N]
First, the bucket can be set to a range of size 10, specifically, set the set b[1] store [1..10] integer, set b[2] Store (10..20] integer, ... Set B[i] Store ((i-1) *10, i*10] integer, i =,.. 100. There are a total of 100 barrels.
Then, scan the A[1..N] from beginning to end, and put each a[i] into the corresponding bucket b[j]. Then the 100 barrels in each bucket in the number of sorting, then can be bubbling, selection, and even fast, in general, any sort method can be.
Finally, the numbers in each bucket are output sequentially, and the numbers in each bucket are output from small to large, so that a sequence of all the numbers is ordered.
Suppose there are n numbers, there are m buckets, and if the numbers are evenly distributed, there is an average number of n/m in each bucket. If
The number in each bucket is quickly sorted, so the complexity of the whole algorithm is
O (n + M * N/m*log (n/m)) = O (n + nlogn-nlogm)
As seen from the above, when M approaches N, the sorting complexity of buckets is close to O (n)
Of course, the calculation of the above complexity is based on the assumption that the input n numbers are evenly distributed. This hypothesis is very strong, the actual application of the effect is not so good. If all the numbers fall into the same bucket, it will degenerate into a general sort.
Some of the above-mentioned sorting algorithms, most of the time complexity are O (N2), there are some sorting algorithm time complexity is O (NLOGN). But the bucket sort can realize the time complexity of O (n). But the downside of bucket sequencing is:
1) First, the space complexity is higher, the additional overhead is required. Sorting has two of the space cost of the array, one for the array to be sorted, and one is the so-called bucket, such as the value to be sorted from 0 to m-1, then need M bucket, this bucket array will be at least m space.
2) The next element to be sorted must be within a certain range and so on.
Bucket sorting is a sort of assignment. Assigning a sort is not a need for a comparison of key codes, but only if you know the specifics of the sequence to be sorted.
The basic idea of assigning a sort: example:
Poker in 52 cards, can be divided into two fields according to the color and face value, the size of the relationship is:
Suit: Plum < block < Hearts < Black Heart
Face value: 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < < J < Q < K < A
If the cards are sorted in ascending order by suit and face value, the following sequence is obtained:
That is, two cards, if the color is different, regardless of the face value, the lower color of the card is less than the height of the color, only in the case of the same color, the size of the relationship is determined by the size of the face value. This is the multi-key ordering.
To get the sort results, we discuss two sorting methods.
Method 1: First the color sorting, divides it into 4 groups, namely Plum Blossom Group, block group, Hearts Group, Black Heart Group. Each group is then sorted by face value, and finally, the 4 groups are connected together.
Method 2: First give 13 numbered groups (2nd, 3rd, ..., A) by 13 denominations, and place the cards in the corresponding numbering group in denominations, divided into 13 piles. Then according to the color given 4 number group (plum, block, Hearts, Black Heart), the group of 2nd in the card out into the corresponding suit group, and then the group of 3rd of the cards are taken out into the corresponding suit group, ..., so that 4 suits are in order, and then, the 4 suit group is connected in turn.
The sequence of n-elements to be ordered consists of the D key code {K1,K2,...,KD}, which means that the sequence pair key code {K1,K2,...,KD} order refers to: for any two records in the sequence R[i] and R[j] (1≤i≤j≤n) to meet the following ordered relationship:
Where K1 is called the most thematic key code, KD is called the most important bit key code.
Two kinds of multi-key code sorting methods:
Multi-key sorting is sorted in two ways: from the most-important-to-most-critical-to-most-key or from the first-to-most-primary-to-maximum-key-code sequence.
Highest priority (most significant Digit first) method, or MSD method:
1) first sorted by K1 group, the sequence is divided into several sub-sequences, the same sequence of records, the key code k1 equal.
2) The groups are divided into subgroups by K2, after which the key codes continue to be grouped until the sub-groups are sorted by the most-important KD.
3) by connecting the groups together, an ordered sequence is obtained. The method of playing poker in the sort of suit and denomination is the MSD method.
Lowest bit priority (Least significant Digit first) method, hereinafter referred to as the LSD method:
1) Start with the KD first, then sort the kd-1, repeat until the K1 sorted by the smallest sub-sequence.
2) Finally, the various sub-sequences are connected, you can get an ordered sequence, poker by color, the value of the method described in the order of the second is the LSD method.
The basic idea of chain Cardinal sort based on LSD method
The idea of "multi-keyword sorting" implements "single-keyword ordering". A single keyword of a numeric or character type can be thought of as a multi-keyword consisting of multiple digits or characters, which can be sorted by the method of allocation-collection, which is called the Cardinal Sort, where each number or character may have a number of values called cardinality. For example, poker's color base is 4, the face value base is 13. In the arrangement of playing cards, can be arranged according to the color, can also be sorted by face value first. According to the color arrangement, first by the red, black, square, flower order into 4 stacks (allocation), and then stacked together in this order (collection), and then in the order of face value divided into 13 stacks (allocation), and then stacked together in this order (collect), so two times allocated and collected can be arranged in order to arrange cards.
Base sort:
are sorted by low, then collected, sorted by high order, then collected, and so on, until the highest bit. Sometimes some properties are prioritized, sorted by low priority, and sorted by high priority. The final order is high priority high in front, high priority is the same low priority high in front. The cardinality sort is based on sorting separately and is collected separately, so it is stable.
Algorithm implementation:
Void Radixsort (Node l[],length,maxradix) { int m,n,k,lsp; K=1;m=1; int temp[10][length-1]; Empty (temp); Empty temporary space while (K<maxradix)//Traverse all keywords {for (int i=0;i<length;i++)//allocation process { if (l[i]<m) Temp[0][n]=l[i]; else lsp= (l[i]/m)%10;//Identify keywords temp[lsp][n]=l[i]; n++; } Collectelement (l,temp); Collection of n=0; m=m*10; k++; }}
Summarize
A summary of the stability, time complexity, and spatial complexity of the various sequencing:
We compare the time complexity function to the situation:
The growth of the time complexity function O (n)
So the sort records for n are larger. The general choice is the sorting method of the time complexity O (nlog2n).
In terms of time complexity:
(1) Order of Square (O (n2))
Types of simple sorting: direct insertion, direct selection and bubble sorting;
(2) Order of linear logarithmic order (O (nlog2n))
Quick sorting, heap sorting, and merge sorting;
(3) O (n1+§)) ordering, § is a constant between 0 and 1.
Hill sort
(4) Order of linear Order (O (n))
The base sort, in addition to the bucket, the box sort.
Description
When the original table is ordered or basically ordered, the direct insertion of sort and bubble sorting will greatly reduce the number of comparisons and move records, time complexity can be reduced to O (n);
While the quick sort is the opposite, when the original table is basically ordered, it will degenerate to bubble sort, and the time complexity is increased to O (N2);
The order of the original table, the simple selection of sorting, heap sorting, merge sorting and the time complexity of the base sort has little effect.
Stability:
The stability of the sorting algorithm: if there are multiple records with the same keyword in the sequence to be sorted, the relative order of the records remains unchanged, the algorithm is said to be stable, and if the relative order of the records is changed after sorting, the algorithm is said to be unstable.
Stability Benefits: If the sorting algorithm is stable, sort from one key, then sort from another key, the result of the first key sort can be used for ordering the second key. The Cardinal sort is like this, first by the low sort, successively by the high order, the low-level same element its sequence is also the same time will not change. In addition, if the sorting algorithm is stable, redundant comparisons can be avoided;
Stable sorting algorithms: bubble sort, insert sort, merge sort, and Cardinal sort
Not a stable sorting algorithm: Select sort, quick sort, hill sort, heap sort
Select the sorting algorithm guidelines:
Each sorting algorithm has its advantages and disadvantages. Therefore, when practical, it is necessary to choose according to different circumstances, or even combine various methods to use.
Select the basis for the sorting algorithm
There are many factors that influence sequencing, and algorithms with low average time complexity are not necessarily optimal. Conversely, sometimes an algorithm with a high average time complexity may be better suited to some particular situation. At the same time, the selection algorithm should also consider its readability, in order to facilitate the maintenance of software. In general, there are four things to consider:
1. The size of the number of records to be sorted n;
2. The size of the data volume of the record itself, that is, the size of other information except the keywords in the record;
3. The structure and distribution of the keywords;
4. Requirements for sorting stability.
Set the number of elements to be sorted n.
1) when n is larger, it should be sorted by the time complexity O (nlog2n): Quick sort, heap sort, or merge sort order.
Quick sort: is currently considered the best method based on the comparison of internal sorting, when the keywords to be sorted are randomly distributed, the average time of the fast sorting is shortest;
Heap sort: If memory space permits and requires stability,
Merge sort: It has a certain number of data movement, so we may have a combination of the insertion sort, first get a certain length of sequence, and then merge, the efficiency will be improved.
2) when n is large, memory space allows, and requires stability = "Merge sort
3) when n is small, it can be directly inserted or directly selected for sorting.
Direct Insert Sort: When the elements are distributed in an orderly manner, inserting the sort directly will significantly reduce the number of comparisons and moves recorded.
Direct selection Sort: element distribution is ordered, if stability is not required, select Direct Select sort
5) generally do not use or not directly use the traditional bubble sort.
6) base sorting
It is a stable sorting algorithm, but it has some limitations:
1, keywords can be decomposed.
2, record the number of key bits less, if dense better
3, if it is a number, it is best to be unsigned, otherwise it will increase the corresponding mapping complexity, you can first
Bucket sort/radix sort (Radix sort)