桶排序
程序员文章站
2022-03-03 09:14:05
...
/** * 桶排序 * <ul> * <li>平均情况:O(N+C)</li> * <li>最好情况:O(N)</li> * <li>最坏情况:O(N+C)</li> * <li>辅助存储:O(N+C)</li> * <li>稳定</li> * <ul> * * @timestamp Mar 13, 2016 7:53:03 PM * @author smallbug */ public class BucketSort { public static void main(String[] args) { int[] data = DataUtil.getData(1000); System.out.println(Arrays.toString(data)); long time = System.currentTimeMillis(); data = bucketSort(data); System.out.println(Arrays.toString(data)); System.out.println("speed " + (System.currentTimeMillis() - time) + " ms"); System.out.println("排序是否成功:" + (DataUtil.verify(data, DataUtil.ASC) ? "是" : "否")); } private static int[] bucketSort(int[] data) { int[] count = new int[data.length];// 计数数组 int[] result = new int[data.length];// 结果数组 for (int i = 0; i < data.length; i++) { count[data[i]]++;// 建立计数表 } for (int i = 1; i < count.length; i++) { count[i] = count[i] + count[i - 1];// 格式化计数表 } for (int i = data.length - 1; i >= 0; i--) { result[--count[data[i]]] = data[i];// 根据计数表排序 } return result; } }
上一篇: C语言实现动态二维数组
下一篇: IT人员都跑哪里去了 IT人员请进