算法学习-Bucket排序

xiaoxiao2021-02-28  121

import java.util.Arrays; public class BucketSort { public static void main(String[] args) { int[] arr = new int[]{7,2,1,0,8,12,6}; long startTime = System.nanoTime(); bucketSort(arr); System.out.println("程序运行时间:"+(System.nanoTime() - startTime)+"ns"); } public static void bucketSort(int[] arr){ int[] a = new int[200]; for(int i = 0; i<arr.length-1; i++){ a[arr[i]] += 1; } System.out.println(Arrays.toString(a)); for(int i = 0; i<a.length; i++){ if(a[i] !=0){ for(int j = 0; j<a[i]; j++) System.out.println(i); } } } }

打印结果:

[1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 0 1 2 7 8 12 程序运行时间:766045ns
转载请注明原文地址: https://www.6miu.com/read-18200.html

最新回复(0)