First, the principle
Bucket sequencing works by dividing the interval into n sub-ranges of the same size, called buckets. The number of n inputs is then stepped into each bucket. Each bucket is sorted individually and then in the order of each barrel
Can be listed in the list of elements.
Second, the complexity of time
Bucket sorting is an inductive result of a sort of pigeon nest. When the values in the array to be sorted are evenly distributed, the bucket sort uses linear time (O (n)). But the bucket sort is not a comparison sort, it is not affected by
O (NLONGN) The lower limit of the effect.
eg: sorts n integers A[1..N] in the range of [1...1000] size. The bucket can be set to a range of size 10, specifically set B[1] store [1..10] integer, collection b[2] Storage (10.. 20] of integers,
i=1,2,,100. A total of 100 buckets and then scan a[i], each a[i] into the corresponding b[j]. Then sort the numbers in each bucket.
Suppose there are n numbers, there are m buckets, and if the numbers are evenly distributed, there is an average number of n/m in each bucket. If the numbers in each bucket are sorted quickly, the complexity of the entire algorithm is
O (N+m*n/m*log (n/m)) = O (N+NLOGN-NLOGM). When m approaches N, the sorting complexity of buckets is close to O (n).
Third, the Code
Import java.util.ArrayList;
Import java.util.Collections;
Import Java.util.Random;
Import java.util.List;
public class Bucketsort {
int bucketsize = 10
int arraySize = 1000;
public static void Main (string[] args) {
TODO auto-generated Method Stub
Bucketsort bs = new Bucketsort ();
int[] array = Bs.getarray ();
Bs.bucketsort (array);
}
Public int[] GetArray () {
int[] arr = new INT[ARRAYSIZE/3];
Random r = new Random ();
for (int i = 0; i < ARRAYSIZE/3; i++)
{
Arr[i] = R.nextint (100000);
}
return arr;
}
public void Bucketsort (int[] a)
{
list<integer> bucket[] = new Arraylist[bucketsize];
for (int i=0; i < a.length; i++)
{
int temp = a[i]/10000;
if (bucket[temp] = = null)
{
BUCKET[TEMP] = new
Arraylist<integer> ();
}
Bucket[temp].add (A[i]);
}
Sort the individual elements in a bucket
for (int j=0;j<bucketsize;j++)
{
Intsertsort (Bucket[j]);
Printlist (Bucket[j]);
}
}
private void Printlist (list<integer> List) {
TODO auto-generated Method Stub
while (List.size () >0)
{
System.out.print (list.remove (0) + "\ T");
}
}
private void Intsertsort (list<integer> List) {
TODO auto-generated Method Stub
Collections.sort (list);
}
}
Bucket sort (bucketsort) (Java)