桶排序:
1.针对少量数据;
2.速度快但是浪费空间;
应用:
1.对少量数据进行排序;
2.可以在对少量数据排序的同时除去重复数据(因为只需要大于零就可以输出);
桶排序原理:
将数据放到已有数组,其数据值对应数组的索引;a[i]>0便输出;
package Sort;
public class BucketSort {
public static void main(String[] args) {
int[] x = { 99, 65, 24, 47, 50, 88,33, 66, 67, 31, 18 };
int[] sorted = bucketSort(x, 99);
for (int i = 0; i < sorted.length; i++)
{
if (sorted[i] > 0)
System.out.println(sorted[i]);
}
}
public static int[] bucketSort(int[] nums, int maxNum){
int[] sorted = new int[maxNum+1];
for(int i=0; i<nums.length; i++){
sorted[nums[i]] = nums[i];//把数据放到对应索引的位置
}
return sorted;
}
}