Here are two ways to make a bucket sort:
A. Simplified bucket sequencing
The code is as follows:
<span style= "FONT-SIZE:18PX;" >/* simplified version of bucket sort */#include <stdio.h>int main () {int book[1001],i,j,t,n;for (i=0;i<=1000;i++) {book[i]=0;} scanf ("%d", &n);//Enter a number n, indicating that there is n number for (i=1;i<=n;i++)//loop read n number, and the bucket sort {scanf ("%d", &t);//read each number into the variable t [t]++; Count, place a small flag on the bucket numbered T}for (i=0;i<=1000;i++)/////////////////////////////To print the bucket number several times (j=1;j<=book[i];j++) printf ("%d", I);} GetChar ();} GetChar (); return 0;} </span>
Two. Full bucket sorting
Base sort (with MSD and LSD)
now to achieve the simplest cardinal sort, that is, the numbers only from small to large sort, there is no category of points.
The idea of a cardinal sort is: sort the digits first, and after the sort is done, count the piles.
Then sort the ordered 10-digit number above, sort it out, then sort the ordered hundred numbers, and so on.
The function plus test code is as follows:
/* Full Bucket sort * * #include <iostream> #include <list>using namespace std; #define RADIX 10/** * Base tree sort (with MSD and LSD) * Now realize the simplest cardinal sort, that is, the number only from small to large sort, no category of the idea of the base sort * is: * First digit of the order, after the sorting is complete, the statistics piles * and then on the top of the order of the 10-digit number sorted, after the completion of the order, then the order of the hundred numbers sorted, An analogy */void radixsort (int arr[],int n) {for (int m = 0;m < radix;m++)//Create RADIX list {//or use list as Barrel list<int> *lists = new list<int>[radix]; for (int i = 0;i < n;i++) {int temp = Arr[i];int loc = 1; for (int t = 1;t < m;t++) {loc *= 10; } lists[(temp/loc%10)].push_back (Arr[i]);//The same value is inserted into the equivalent list} int j = 0; Merge radix list for (i = 0;i < 10;i++) {if (lists[i].size ()! = 0) {list<int>::i Terator it = Lists[i].begin (); while (It! = Lists[i].end ()) {arr[j] = *it; it++; j + +; }}}}}void main () {int arr[] = {278,109,63,930,589,184,505,269,8,83};for (int i = 0;i < sizeof (arr)/sizeof (int); ++i) {cout<<arr[i]<< "";} cout<<endl; Radixsort (arr,sizeof (arr)/sizeof (int)); for (i = 0;i < sizeof (arr)/sizeof (int); ++i) {cout<<arr[i]<< "";} Cout<<endl;}
Running results such as:
I am very much looking forward to your criticisms and suggestions ~ Thank you all
Sort Cardinal sort (count sort, bucket sort)