import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
* Copyright 2015年5月12日 by
* All Rights Reserved
*
* @Package
* @Title: BucketSort.java
* @author
* @date 2015年5月12日 下午9:27:49
*/
public class BucketSort
{
public static void main(String[] args)
{
int k = 0;
while (k < 1000)
{
BucketSort bs = new BucketSort();
Integer[] toBeSortedData = bs.createArray(10, 100);
// Integer[] toBeSortedData = { -419804243, -1912988136, -574062376,
// -81486055, 1931432974 };
System.out.println("\n 排序前的数组:");
ArrayUtil.printArray(toBeSortedData);
Integer[] haveSoredArray = bs.sort(toBeSortedData);
System.out.println("\n 排序后的数组:");
ArrayUtil.printArray(haveSoredArray);
if (ArrayUtil.isSortedArray(haveSoredArray))
{
System.out.println("\n 排序算法正确");
}
else
{
System.err.println("\n 排序算法错误");
}
k++;
}
}
@Override
public Integer[] sort(Integer[] beSorted)
{
int maxValue = getMaxValue(beSorted);
int minValue = getMinValue(beSorted);
int bucketNumbers = getBucketNumbers();
int skipValue = (maxValue - minValue) / bucketNumbers + 1;
List[] beSettingArray = new List[skipValue];
int startValue = 0;
for (int i = 0; i < skipValue; i++)
{
ArrayList<Integer> currentArray = new ArrayList<>();
int endValue = startValue + skipValue;
for (int j = 0; j < beSorted.length; j++)
{
int value = beSorted[j] - minValue;
if (startValue <= value && value <= endValue)
{
currentArray.add(value);
}
}
Collections.sort(currentArray);
beSettingArray[i] = currentArray;
startValue = endValue + 1;
}
for (int i = 0,k=0; i < beSettingArray.length; i++)
{
List<Integer> currentL = beSettingArray[i];
for (int j=0;j<currentL.size();j++)
{
beSorted[k]=currentL.get(j);
k++;
}
}
return beSorted;
}
private Integer[] bucketSort(Integer[] bucketArray)
{
return null;
}
private int getBucketNumbers()
{
return 3;
}
private Integer getMaxValue(Integer[] beSorted)
{
int max = Integer.MIN_VALUE;
for (Integer value : beSorted)
{
if (value >= max)
{
max = value;
}
}
return max;
}
private Integer getMinValue(Integer[] beSorted)
{
int min = Integer.MAX_VALUE;
for (Integer value : beSorted)
{
if (value <= min)
{
min = value;
}
}
return min;
}
private Integer[] createArray(int length, int max)
{
Integer[] beSorted = new Integer[length];
Random random = new Random();
for (int i = 0; i < length; i++)
{
beSorted[i] = random.nextInt(max);
}
return beSorted;
}
}
package
import java.util.Random;
/**
* Copyright 2015年5月3日 by
*
* All Rights Reserved
*
* @Package
* @Title: ArrayUtil.java
* @author
* @date 2015年5月3日 上午9:01:23
*/
public class ArrayUtil
{
public static boolean isSortedArray(int[] test)
{
int i = 0;
int j = 1;
while (j < test.length)
{
if (test[i] > test[j])
{
return false;
}
i++;
j++;
}
return true;
}
public static int[] createIntArrays(Random random, int size)
{
int[] intArray = new int[size];
for (int i = 0; i < intArray.length; i++)
{
intArray[i] = random.nextInt();
}
return intArray;
}
public static void printArray(int[] t)
{
System.out.println("array size is: " + t.length + " values are:");
for (int i : t)
{
System.out.print(i + " ");
}
}
public static boolean isSortedArray(Integer[] test)
{
int i = 0;
int j = 1;
while (j < test.length)
{
if (test[i] > test[j])
{
System.out.println("\n 前一个值:" + test[i] + "大于" + "后一个值:"
+ test[j]);
return false;
}
i++;
j++;
}
return true;
}
public static Integer[] createIntergerArrays(Random random, int size)
{
Integer[] intArray = new Integer[size];
for (int i = 0; i < intArray.length; i++)
{
intArray[i] = random.nextInt();
}
return intArray;
}
public static void printArray(Integer[] t)
{
System.out.println("array size is: " + t.length + " values are:");
for (int i : t)
{
System.out.print(i + " ");
}
}
public static void swap(Integer[] beSorted, int big, int little)
{
int temp = beSorted[big];
beSorted[big] = beSorted[little];
beSorted[little] = temp;
}
}