C + + cardinal ordering
Everyone good, today brings is the realization of their own by C + + to complete the order of Cardinal. In the learning process of data structure, algorithm analysis and program design, we often cannot avoid the algorithm of sorting. The sorting algorithm is one of the most frequently used algorithms in the process of programming, and its input is a set of unordered sequences, Requires an ordered set of sequences to be output in ascending or descending order. For such algorithms as binary lookup, the input is ordered sequence, which is to first sort after lookup, this shows the importance of sorting algorithm.
Well-known sort algorithms have bubble sort, and select sort, insert sort. Advanced some have quick sort, hill sort, heap sort, merge sort, cardinality sort etc. The algorithm with time complexity O (N*LOGN) has fast sorting, merging sort and heap ordering, among which the fast sorting is the most widely used. The algorithm with time complexity O (n2) has bubble sort, select sort and insert sort, etc.
Cardinality ordering is a kind of non comparison sorting algorithm, which is based on bucket sorting. They have been used in the sort of old-fashioned punch cards before the advent of modern computers.
The basic idea of Cardinal order is: a total of 10 "barrels", represents the 0~9 of each digit. In each bucket, organize a priority queue, for the input of a series of positive integers and 0, according to the size of the number of digits into 10 "barrels." and then iterate through each "bucket" and adjust it according to the size of the 10-bit relationship, Followed by hundreds, thousands ... Until the maximum number of digits is reached. With the legend, we can understand this algorithm:
In the C + + implementation, I used the queue this data structure as each "bucket" organization way, because the number is always taken from the bottom, and put the number of this is put into the top of the bucket, which is the queue with the head of the team, the team tail in the same way. For 10 "barrels" of tissue, vector vectors are used. This program supports, An unlimited number of input sequences, and a non-negative sort in the range of the int data type, that determines the number of sequential journeys based on the number of digits of the maximum number. Change the data type from int to long or long long to sort the larger integers.
The sort functions are as follows:
1 vector<int> radix_sort (vector<int> in) 2 {3 vector<queue<int>> bucket (10);
10 barrels for a vector, each barrel is also a queue 4 int max_value = in.at (0);
5 6 for (auto &i:in) 7 {8 if (i > Max_value) 9 max_value = i;
10}//Find the input maximum element of int n = 0; for (; max_value!= 0; Max_value/=, ++n) 15; Gets the maximum number of digits, which is also the sort of trip for (auto &i:in)-bucket.at (0). push (i); All put in the first bucket int i = 0; The number of flight control variables int m = 0; The control variables related to the extraction of each number of digits are int k = 0; Bucket number control variable int x = 0; The size of the bucket, because the dynamic change of the container, the iterator will be invalidated, do not use the iterator of int y = 0;
Barrel internal control variable Int j = 0; num int item = 0; Bucket elements for (; i < n; ++i)//Cycle count for (k = 0; K <10;
++K)//traverse each bucket to {x = bucket.at (k). Size ();
if (!x) continue; for (y = 0; y < x; ++y)//traverse the elements of the queue in the bucket {The item = j = bucket.at (k). fron
T ();
(m = i > 0;--m)//extract each position J/= 10; The switch (j% 10)//entered the corresponding bucket {case 0:47 bucket.at (0
). push (item);
a break;
Case 1:51 bucket.at (1). push (item);
The break;
Case 2:55 bucket.at (2). push (item);
a break;
The Case 3:59 bucket.at (3). push (item);
break;
The Case 4:63 bucket.at (4). push (item);
The break;
The Case 5:67 bucket.at (5). push (item);
a break; Case 6:71 bucket.at (6). push (item);
a break;
7:75 bucket.at (7). push (item);
a break;
The case 8:79 bucket.at (8). push (item);
The break;
Bayi Case 9:83 bucket.at (9). push (item);
The break; The default://anomaly detection, capture and processing throw
Runtime_error ("error!"); Runtime_error err) cout {err.what () &L
t;< Endl;
The exit (exit_failure);
Bucket.at (k). Pop (); {} 102 vector<int> out;
Defines a new vector that collects all the bucket data as the final result (i = 0; i < ++i) the bucket.at (i), the 108, the num = (i). Size (); 109 for (int ai = 0; ai < num; ++ai) the Out.push_back (bucket.at (i)). Front ());
112 bucket.at (i)-pop (); 113} 114}//Sort result to a vector of 116 return out;
Returns this ordered sequence of 117 118}
algorithm to get the correct result, note that the order of the elements of the same bucket is incremented from bottom to top, which starts with the "bucket" representing 0 when traversing and takes from the bucket Elements are guaranteed from the bottom. Again, the last thing you need to do is take the order out of the bucket.