Bucket Sort
Bucket sequencing is an O (n) algorithm, which belongs to the algorithm of exchanging space for time, and summarizes its use can have the following points
1. Implement the sort by rule.
2. Find the maximum spacing between data.
3. Find the maximum number of three spaces between data.
4. Find a sorting algorithm with time complexity of O (n).
...... such as
--------------------------------------------------------------------------------------------------------------- ----------------------------------------
Algorithm ideas:
1. Pending update ...
--------------------------------------------------------------------------------------------------------------- ----------------------------------------
On the code:
Class for storing bucket data:
public class Tagsbucket {
Boolean isValid;
int Max;
int min;
void Add (int n)
{
if (n<min)
{
min=n;
}
if (N>max)
{
max=n;
}
}
public Boolean isValid () {
return isValid;
}
public void Setvalid (Boolean isValid) {
this.isvalid = isValid;
}
public int Getmax () {
return max;
}
public void Setmax (int max) {
this.max = max;
}
public int getmin () {
return min;
}
public void setmin (int min) {
this.min = min;
}
}
Code implementation:
Import java.util.ArrayList; public class Buckstsort {/** * @param args */public static void main (string[] args) {//TODO auto-generated Me
Thod stub int[] data = new int[] {1, 14, 31, 22, 66};
Bucketsort (data, data.length); } private static void Bucketsort (int[] data, int len) {//TODO auto-generated Method Stub//create object with initial validity of false A
rraylist<tagsbucket> arr = new arraylist<tagsbucket> ();
for (int i = 0; i < len; i++) {Tagsbucket temp = new Tagsbucket ();
Temp.setvalid (FALSE);
Arr.add (temp);
}//Put the data in the corresponding Bucket//first to find the corresponding bucket, then you need the maximum and minimum value of the array int min = data[0];
int max = data[0];
for (int i = 1; i < Len; i++) {if (Data[i]>max) {max=data[i];
} if (data[i]<min) {min=data[i];
}}//Initialize the value of Max and Min in the bucket for (int k=0;k<len;k++) {///bucket int bucket= (data[k]-min) *len/(max-min);
System.out.println (k + "----" +bucket);
if (Bucket>=len) {bucket=bucket-1;
} /* SYSTEM.OUT.PRINTLN (bucket* (max-min)/len);
System.out.println (min+bucket* (max-min)/len);
System.out.println ((min+bucket* (max-min)/len)); */Arr.get (Bucket). Setmin (Data[k]);
Arr.get (Bucket). Setmax (Data[k]);
}//Next put on the corresponding bucket for (int j=0;j<len;j++) {//Bucket int bucket= (data[j]-min) *len/(max-min);
if (Bucket>=len) {bucket=bucket-1;
} arr.get (Bucket). Setvalid (True);
Arr.get (Bucket). Add (Data[j]);
}//output result int b=0;
int result=0;
for (int a=0;a<len;a++) {if (Arr.get (a). IsValid ()) {int Resulttemp=arr.get (a). Getmin ()-arr.get (b). Getmax ();
if (Resulttemp>result) {result=resulttemp;
} b=a;
}} System.out.println (Result);
}
}Output Result:
0----0
1----1
2----2
3----1
4----5
35