1. Counting sort
Basic idea: For each INPUT element x, determine the number of elements less than (or equal to) x, you can determine where the x is stored.
Features: stable sequencing
#include <iostream>#include<vector>using namespacestd;voidCount_sort (vector<int> a,vector<int> &b,intk) {Vector<int> C (k +1,0); for(intj=0; J<a.size (); j + +) {C[a[j]]= c[a[j]]+1;//or c[a[j]]++; C[i] contains the number of elements equal to I } for(intI=1; i<=k;i++) {C[i]= C[i] +c[i-1];//C[i] contains the number of elements that are not greater than I } for(intJ=a.size ()-1; j>=0; j--) {B[c[a[j]]-1] = A[j];//C[a[j]] Description is the position, subscript should again-1C[A[J]] = C[a[j]]-1; }}intmain () {vector<int> a{9,2,4,3,5,6,8,7,1,9,6}; Vector<int> B (a.size (),0); Count_sort (A, B,9); for(ConstAuto &c:b) {cout<<c<<","; } cout<<Endl; return 0;}
2. Base sorting
Basic idea: Use a stable sorting algorithm to sort each of the data from low to high.
#include <iostream>#include<vector>using namespacestd;voidCount_sort (vector<int> &a,intIintK) {//I represents the first bit of data, and K represents the maximum value per bitvector<int> B (a.size (),0);//save sorted avector<int> C (k +1,0); Vector<int> D (a.size (),0);//Save the first bit of each element for(intj=0; J<a.size (); j + +){ intR=A[j]; intX=i;//cannot change the value of I while(x>0) {D[j]= r%Ten; R= r/Ten; --x; } } for(intj=0; J<a.size (); j + +) {C[d[j]]++;//use D instead of a to calculate C } for(intI=1; i<=k;i++) {C[i]= c[i]+c[i-1]; } for(intJ=a.size ()-1; j>=0; j--) {B[c[d[j]]-1] = A[j];//Save A[j] instead of d[j]C[D[J] [=c[d[j]]-1; } A=b;}voidRadix_sort (vector<int> &a,intd) { for(intI=1; i<=d;i++) {Count_sort (a,i,9); }}intmain () {vector<int> a{329,457,657,839,436,720,355, the}; Radix_sort (A,3); for(ConstAuto &c:a) {cout<<c<<","; } cout<<Endl; return 0;}
3. Sorting buckets
The count sort assumes that the data belongs to an integer within a small interval, and the bucket ordering assumes that the input is produced by a random process that distributes the elements evenly and independently across the [0,1] interval. Bucket sorting divides the [0,1] interval into n equal-sized sub-ranges. That is, the bucket. Then put n inputs into each bucket. Sort the elements in each bucket, then loop through each bucket sequentially to get an ordered sequence.
#include <iostream>#include<vector>#include<algorithm>using namespacestd;voidBucket_sort (vector<Double>a) { intn =a.size (); Vector<Double>B[n]; for(intI=0; i<n;i++) {B[static_cast<int> (N*a[i])].push_back (A[i]);//put elements in a bucket } for(intI=0; i<n;i++) {sort (B[i].begin (), B[i].end ());//sort the elements in each bucket } for(ConstAuto &x:b) { for(ConstAuto &y:x) {cout<<y<<","; }} cout<<Endl;}intMainintargcChar Const*argv[]) {Vector<Double> a{0.78,0.17,0.39,0.26,0.72,0.94,0.21,0.12,0.23,0.68}; Bucket_sort (a); return 0;}
The linear sorting algorithm uses the most important of all, taking full advantage of the special nature of the data to achieve the best results . (source)
Linear time Sequencing