Programming theory to the extreme, core non-code, that is thought.
So, a true programmer is also a person with a unique and intelligent mind (note and smart distinction).
Each algorithm is a kind of wisdom of condensation or extraction, it is worth learning to improve ourselves, pioneering ideas, more importantly, the conversion of thinking angle.
In fact, most of us live under the "Default state". Without discovering your own unique set of options-----ideas.
Come to the chase (hehe! Restore default state), the following learning cardinality is sorted.
"1" Cardinal sort
In the past, all sorts of sorting algorithms are sorted by comparing data size to order data sequence.
But the base sort is no longer the same, so what sort of strategy does the base sort use?
Brief overview: The cardinality sort is accomplished by the allocation and collection processes. And how is this thought supposed to be understood? Take a look at the example below.
(1) The pseudo-arranged data sequence is as follows:
73 22 93 43 55 14 28 65 39 81
First, based on the numeric value of single digits, they are assigned to buckets numbered 0 to 9 (single digit versus bucket number one by one) when traversing the data.
The result of the allocation (logical imagination) is as follows:
After the assignment ends. Next, the data in all the buckets is collected in order from small to large (from top to bottom in the bucket), and the following data sequences are still unordered:
81 22 73 93 43 14 55 65 28 39
Next, assign again, this time based on the 10-bit value (IBID.), the allocation result (logical imagination) as shown:
After the assignment ends. Next, the data in all buckets (the same principle as above) is then re-collected serially, resulting in the following data series:
14 22 28 39 43 55 65 73 81 93
As you can see, the original unordered data sequence is sorted. If the sorted data series has more than three digits of data, repeat the above action until the maximum number of digits is reached.
So, so far, do you think you are a careful person? Don't answer me without hesitation. No matter what kind of questions to answer, you have to do the heart faster than the head, head than Zuikuai.
Have a closer look at your doubts about the whole sequencing process? Can't you see it? Think I did a good job? Or did you read it before?
If you see here really do not realize or find this problem, then I tell you: quietly to find a corner squat with the little finger to draw a circle (good introspection).
Cross-examine: Observe the order of 73 93 433 data in the original unordered data series, after the first (according to the single digit value, they should be in the same bucket) after allocation,
In the barrel of the order from the bottom to the top should be 73 93 43 (that is, it is loaded late on the top, corresponding to our logical imagination above should be 43 93 73), right? This should be a thought, you know? That should be the case in theory.
But, however, it is clear that the order of the three in bucket 3rd is reversed. Don't you see that? Or did you find that you don't like to talk about (count me laughable)?
In fact, this is the reason for the stability of the cardinal order (when the allocation is made from the bottom to the first), see the detailed analysis below.
One more question: since we can do such a collection from the lowest to the highest, can we proceed from the highest to the lowest bit? The answer is perfectly acceptable.
Based on two different sort orders, we divide the cardinality sort into LSD (Least significant digital) or MSD (most significant digital),
The order of LSD starts with the rightmost (low) of the value, while the MSD is the opposite, starting with the leftmost (high) value.
Note that the radix sort of LSD applies to numbers with fewer digits, and if the number of bits is more, the efficiency of using an MSD is better.
MSD, in contrast to LSD, is assigned by the high number as the base, but does not merge back into an array immediately after the allocation, but instead creates a "sub-bucket" in each "bucket", assigning the value in each bucket to the "sub-bucket" in terms of the next digit.
Once the minimum number of digits has been allocated, it is merged back into a single array.
(2) We consider the sort of poker as the primary key that consists of two data items in the suit and face value.
Requirements are as follows:
Color Order: Plum < block < Hearts < Spades
Denomination Order: 2<3<4<...<10<j<q<k<a
Then, to arrange a deck of cards in the following order:
Plum 2, ..., plum blossom A, block 2, ..., square A, Hearts 2, ..., Hearts A, spades 2, ..., spades a.
There are two kinds of sorting methods:
<1> The stacks are divided into four stacks, and the piles are collected, and then each pile is arranged in denominations from small to large, and then stacked in stacks by the suit. ----is called the "highest bit first" (MSD) method.
<2> first by the face value from small to large arranged into 13 piles, then from small to large collection, and then divided into four stacks according to the color, the final order collected. ----is called the "lowest bit first" (LSD) method.
"2" Code implementation
(1) The MSD method is implemented
The highest-bit precedence method is usually a recursive process:
<1> first according to the highest bit key code K1 sorting, get a number of object groups, object group each object has the same key code K1.
<2> then sorts the objects in each group according to the key code K2, and then divides them into smaller subgroups, each of which has the same K1 and K2 values as the K2 value.
<3> repeat until the key-code KD finishes sorting.
<4> Finally, the objects in all subgroups are concatenated in turn to get an ordered sequence of objects.
(2) LSD method realization
The lowest bit priority method first sorts all the objects according to the lowest bit key KD,
Then according to sub-low key code Kd-1 the results of the previous trip sorted again,
Repeat until the last order is completed according to the key code K1, and an ordered sequence can be obtained.
When you sort each key code using this sort method, you do not need to group again, but the entire group of objects.
The sample code is as follows:
8-3. Base sort. cpp: The entry point that defines the console application. #include "stdafx.h" #include <iostream>using namespace std;int getdigit (int x,int d)//get digit from Right{int a[] ={1,1,10,100};return (X/a[d])%10;} void Lsdradix_sort (int a[],int begin,int end,int d) {int count[10];int *bucket=new int[end-begin+1];for (int k=1;k<=d; k++) {//init array of countfor (int i=0;i<10;i++) {count[i]=0;} Sum each digits numbersfor (int i=begin;i<=end;i++) {count[getdigit (a[i],k)]++;} Ensure index in bucket (!!! Pay attention to repeat numbers) for (int i=1;i<10;i++) {count[i]=count[i]+count[i-1];} Fill bucket arrayfor (int i=end;i>=begin;i--) {bucket[count[getdigit (a[i],k)]-1]=a[i];count[getdigit (a[i],k)]- -;} Gether the number from bucketfor (int i=begin,j=0;i<=end;i++,j++) {a[i]=bucket[j];}} delete[] bucket;//!!!} void Msdradix_sort (int a[],int begin,int end,int d) {int count[10];int *bucket=new int[end-begin+1];//init array of countf or (int i=0;i<10;i++) {count[i]=0;} Sum each digits numbersfor (int i=begin;i<=end;i++) {count[getdigit (a[i],d)]++;} Ensure index in bucket (!!! Pay attention to repeat numbers) for (int i=1;i<10;i++) {count[i]=count[i]+count[i-1];} Fill bucket arrayfor (int i=end;i>=begin;i--) {bucket[count[getdigit (a[i],d)]-1]=a[i];count[getdigit (a[i],d)]- -;} Gether the number from bucketfor (int i=begin,j=0;i<=end;i++,j++) {a[i]=bucket[j];} delete[] bucket;//!!! Sort group Data recursivilyfor (int i=0;i<10;i++) {int P1=begin+count[i];int p2=begin+count[i]-1;if (p1<p2& &d>1) {msdradix_sort (a,p1,p2,d-1);}}} int _tmain (int argc, _tchar* argv[]) {int array[]={329,457,657,839,436,720,355};lsdradix_sort (array,0,6,3); for (int i= 0;i<7;i++) {cout<<array[i]<< "";} Cout<<endl;int Array1[]={329,457,657,839,436,720,355};msdradix_sort (array1,0,6,3); for (int i=0;i<7;i++) { cout<<array1[i]<< "";} Cout<<endl;return 0;}
Stability analysis of "3" Cardinal sort
The Cardinal sort is a stable sort algorithm, so how do you understand what it calls stable characteristics?
For example: We have the following data sequence:
Below Select the LSD logic demo
For the first time, the number of digits is assigned, as shown in the following:
The results of the collected data are as follows:
The second time is assigned by a 10-bit value, as shown in the following:
The results of the collected data are as follows:
Note: The assignment is done from the bottom of the desired data sequence, and is assigned to the first place successively.
All right! Sort end. Believe it must be at a glance. Do not repeat here.
8-3. Detailed base Order