Go to Baidu for the specific code, understand the algorithm and write the code yourself.
Barrel classification
(barrel
Sort) or so-called box sorting, is a sorting algorithm that works by dividing an array into a limited number of buckets. Each bucket is sorted separately (other sorting algorithms can be used, or bucket sorting can be continued recursively). Bucket sorting is the inductive result of pigeon nest sorting. When the values to be sorted in the array are evenly distributed, bucket sorting uses linear time (θ (n)). But bucket sorting is not.
Compared with the order, he is not affected.
O(n
log
n)
The influence of the lower limit.
1.n numbers (plastic)
2. Find the maximum number of m..
3. Because the numbers are all composed of 0 to 9, make 10 bucket, 0 to 9, and put n numbers in the bucket in turn.
3. 1
Starting with single digits, starting with each number, I (I
3.2
Starting from 0 barrels, take out the numbers in all barrels as new numbers.
3.2
Repeat the above two steps until m is a number.
4. The last sort is the array from small to large.
Because it is data sorting, the key value of the set bucket is 0~9*** ten buckets. Every scan starts with the last digit of the data. If the value of this digit is equal to the key value of the bucket, the data is put into the bucket. The bucket can be regarded as an ordered linked list, and the elements entered later are arranged behind the data entered earlier, and a scan is not considered until all the data are scanned. After that, the penultimate scanning is carried out in turn, and the scanning is started according to the key value of the bucket, and the data whose numerical value is equal to the key value of the bucket is also put into the bucket. Until all the most significant figures of the data are scanned. After the last scan, the key value of the bucket goes from low to high. The result of stringing these linked lists is the sorting of the original data from small to large.
-
Code (from data structure algorithm Visual. C.6.0 assembly)
empty
Master ()
{cout & lt& lt”bucketsort.cpp running result: \ n ";
Internationalorganizations (same as international organizations)
Array [size];
Cout < < "original array: \ n";
srand(time(0));
for(int
I = 0; I< size; ++i)
{ array[I]= rand()0;
cout & lt& ltsetw(3)& lt; & ltarray[I]; }
cout & lt& lt\ n ';
Cout & lt& lt "demonstration of sorting process: \ n";
BucketSort (array);
Cout < < "sort array: \ n";
for(int
j = 0; J< size; ++j)
cout & lt& ltsetw(3)& lt; & ltarray[j];
cout & lt& ltendlCIN . get();
}
//
Bucket sorting algorithm
empty
bucketSort(int
a[])
{int
totalDigits,bucket[ 10][SIZE]= { 0 };
totalDigits=numberOfDigits(a,SIZE);
for(int
I = 1; I<= totalDigits++i)
{
distributeElements(a,bucket,I);
Collected elements (a, bucket);
//Initialize the bucket array to 0
If (me! = total digits)
ZeroBucket;
for(int
j = 0; J< size; ++j)
cout & lt& ltsetw(3)& lt; & lta[j];
cout & lt& ltendl}
}
//Determine the maximum number of digits of a single subscript array.
Internationalorganizations (same as international organizations)
numberOfDigits(int
b[],int
Array size)
{
Internationalorganizations (same as international organizations)
Maximum =b[0], digits = 0;
for(int
I = 1; I & ltarraySize++i)
if(b[I]& gt; Max)
Maximum = b [I];
And (the biggest! =0)
{
++numbers;
Maximum/=10; }
return
Numbers;
}
//
Place each value of a single subscript array in a row of the bucket array.
empty
Distributed element (int
a[],int
Bucket [] [Size], integer
Numbers)
{int
Divider = 10, bucketNumber, elementNumber
for(int
I = 1; I < number; ++i)
Divider * =10;
for(int
k = 0; K< size; ++k)
{
Bucket number =(a[k]% divisor -a[k]% (divisor/10))/ (divisor/10); //Found that m is a number.
element number = ++ buckets[bucket number][0]; //buckets[bucketNumber][0]
Represents the number of bits in the bucket.
buckets[bucket number][element number]= a[k]; //Put it in the corresponding bucket.
}
}
//Copy the values of the bucket array back to the original array.
empty
Collected elements (int
a[],int
Bucket [] [size])
{int
Subscript = 0;
for(int
I = 0; I< 10; ++i)
for(int
j = 1; j & lt= buckets[I][0]; ++j)
A[ subscript++] = bucket [I] [J];
}
//Initialize the bucket array to 0
empty
Zero bucket (integer)
Bucket [] [size])
{for(int
I = 0; I< 10; ++i)
for(int
j = 0; J< size; ++j)
buckets[I][j]= 0; }