Java实现桶排序

详细讲解见《算法导论》8.4节——桶排序。

Java代码如下:

package com.liyuncong.sort.sort_linetime;

import java.util.LinkedList;

import com.liyuncong.sort.sort_common.impl.InsertSort;
/**
 * 桶排序假设输入元素均匀而独立的分布在区间[0,1)上;
 * 桶排序的核心思想是,将[0,1)分为n个大小相同的子区间,
 * 上一个区间里的元素都比下一个区间里的元素小,然后对
 * 所有区间里的元素排序,最后顺序输出所有区间里的元素,
 * 达到对所有元素排序的目的。
 * @author yuncong
 *
 */
public class BucketSort {
	public void sort(Double[] a) {
		int n = a.length;
		
		/**
		 * 创建链表(桶)集合并初始化,集合中的链表用于存放相应的元素
		 */
		int bucketNum = 10; // 桶数
		LinkedList<LinkedList<Double>> buckets = new LinkedList<LinkedList<Double>>();
		for(int i = 0; i < bucketNum; i++){
			LinkedList<Double> bucket = new LinkedList<Double>();
			buckets.add(bucket);
		}
		// 把元素放进相应的桶中
		for(int i = 0; i < n; i++){
			int index = (int) (a[i] * bucketNum);
			buckets.get(index).add(a[i]);
		}
		// 对每个桶中的元素排序,并放进a中
		int index = 0;
		for (LinkedList<Double> linkedList : buckets) {
			int size = linkedList.size();
			if (size == 0) {
				continue;
			}
			/**
			 * 把LinkedList<Double>转化为Double[]的原因是,之前已经实现了
			 * 对数组进行排序的算法
			 */
			Double[] temp = new Double[size];
			for (int i = 0; i < temp.length; i++) {
				temp[i] = linkedList.get(i);
			}
			// 利用插入排序对temp排序
			new InsertSort().sort(temp);
			for (int i = 0; i < temp.length; i++) {
				a[index] = temp[i];
				index++;
			}
		}
		
	}
	
	public static void main(String[] args) {
		Double[] a = new Double[]{0.3, 0.6, 0.5};
		new BucketSort().sort(a);
		for (int i = 0; i < a.length; i++) {
			System.out.println(a[i]);
		}
	}

}


对桶中数据排序的插入排序实现见另一篇文章——Java实现插入排序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值