Basic requirements:
Input n integers of m degree from the keyboard, and output the ascending arrangement of these integers.
Specific requirements:
- The data structure used is queue, which is realized by sequence queue
- Good human-computer interaction
- Be able to output the distribution and collection of each trip
Basic concepts:
Cardinality sorting belongs to distributive sorting, also known as bucket method. Through the query of key value, the elements to be sorted are assigned to some "buckets" to achieve the purpose of sorting.
Specific ideas:
Take reshaping as an example, divide the reshaping decimal by each bit, and then compare from low to high
It is mainly divided into two processes:
For allocation, start from bits, and put them into buckets 0-9 according to bit value (0-9)
Collect, and then put the data placed in buckets 0-9 into the array in order
Repeat the allocation and collection process. The number of repeats is from bit to the highest bit
Code implementation:
#include<stdio.h> #include<stdlib.h> #include<string.h> #Define radius 10 / / represents the radius (10) queues typedef int QElemType; typedef struct { QElemType *base; //Initial dynamic allocation of space, storage of data int front; //Head pointer, pointing to the queue head element, used to move int rear; //Tail pointer, pointing to the next position of the queue tail element, to determine the length } SqQueue; //distribute for n th distribution //The original data is stored in the Q.base array //ArrType is an array of type SqQueue, void distribute(SqQueue &Q,int n,SqQueue ArrType[]) { //quot- quotient //rem - stores the value of the nth bit (i.e. the first, second, and Nth bit) of each data int i,c,temp,quot,rem; //for empty Radix (10) queues for(i=0; i<Radix; i++) ArrType[i].front=0; //for processes the n th bit of each data for(i=0; i<Q.rear; i++) { //Stage i data to quot e quot=Q.base[i]; c=0; //while calculates the value of the nth bit of the ith data and saves it to rem while(c<n) { c++; rem=quot%Radix; quot/=Radix; } //Copy the data whose n th bit is rem to the remth queue //That is, according to the nth digit rem of data, data is allocated to the end of the remth queue ArrType[rem].base[ArrType[rem].front++]=Q.base[i]; } printf("The first%d After a trip assignment:\n",n); for(i=0; i<Radix; i++) { temp=ArrType[i].front; //if the i-th queue is not empty if(temp>0) { printf("queue[%d]Head pointer front->",i); for(c=0; c<temp-1; c++) printf("%d-",ArrType[i].base[c]); printf("%d<-queue[%d]Tail pointer rear\n",ArrType[i].base[temp-1],i); } } } //collect for the n th collection //ArrType saves the results of the n-1 assignment void collect(SqQueue &Q,int n,SqQueue ArrType[]) { //Collected records the amount of data collected int i,collected=0; //for collects data from array ArrType into int array Q.base for(i=0; i<Radix; i++) { //if the i-th queue is not empty, collect its data if(ArrType[i].front!=0) { //Collect (simply copy to Q.base array) memcpy(Q.base+collected,ArrType[i].base,ArrType[i].front*sizeof(int)); collected+=ArrType[i].front; } } printf("The first%d After the collection:\n",n); for(i=0; i<=Q.front-1; i++) { printf("%d ",Q.base[i]); } printf("\n\n"); } //Calculate the initial length of each queue (1 / radius of the amount of data) void Ord(SqQueue &Q,SqQueue ArrType[],int M) { int i; int temp=M; for(i=0; i<Radix; i++) { //Allocate space for the i th queue ArrType[i].base=(int*)calloc(temp,sizeof(int)); ArrType[i].rear=temp; } } //for M (M is the length of each data input) times allocation and collection void RadixSort(SqQueue Q,int M,SqQueue ArrType[]) { for(int i=1; i<=M; i++) { distribute(Q,i,ArrType); collect(Q,i,ArrType); } } int main() { int i,M; SqQueue Q,ArrType[Radix]; printf("Please enter the length of each data:"); scanf("%d",&M); printf("Please enter the total number of data:"); scanf("%d",&Q.rear); Q.base=(int*)calloc(Q.rear,sizeof(int)); Q.front=0; printf("Please input%d The length is%d Data:\n",Q.rear,M); for(i=0; i<Q.rear; i++) scanf("%d",&Q.base[Q.front++]); printf("\n"); Ord(Q,ArrType,M); RadixSort(Q,M,ArrType); //The following free releases dynamically allocated memory free(Q.base); for(i=0; i<Radix; i++) free(ArrType[i].base); system("PAUSE"); return 0; }
Data processing:
Last word:
The cardinality sort method is a stable sort, whose time complexity is O (nlog(r)m), where r is the cardinality and M is the heap number
In some cases, the efficiency of Radix sorting is higher than that of other stable sorting methods.