2--Bucket ordering of algorithms

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.