Simple bucket sort (Bucket Sort)

 

1. The basic idea

The bucket sort elements is ordered to be set in the same value range stored in the same bucket 1 .

2. Algorithm Design 2

Suppose you have a class five people, the end of the exam, they are 5 points, 2 points, 4 points, 5 points, 8 points (out of 10 points). These scores will need from small to large

First, we apply a array of size 11 int bucket [11]. In the beginning we regard the elements of the array bucket [0] ~ bucket [10] are initialized to 0, which means no one has to get these scores are too. E.g. bucket [0] is represented by No 0 points, bucket [8] is represented by 8 points No.
initialization

  1. Start processing each fraction. A first individual score of 5 points, then the bucket will [5] 1 plus the value in the original basis, i.e. bucket [5] is a value from 0 to 1, it indicates 5 minutes appeared once
    1
  2. A second individual score of 2 points, then it will bucket [2] increases the value of 1 in the original basis, i.e. bucket [2] is a value from 0 to 1, two points represents once occurred
    2
  3. The third score of 4 points, and so on, can be obtained
    3
  4. The fourth individual score of 5 points, pay attention, and we value the bucket [5] plus 1 on the basis of the original, then the bucket [5] value from 1 to 2, represents 5 points appear twice
    4
  5. The last person to score 8 points, was
    5
    the end, there have been only need to print out scores

3. Code

public class SimpleBucketSort {
    public static void main(String[] args) {
        // 下面是学生取得的分数,假设分数最大为10
        int[] a = {5,3,5,2,8};
        // 创建11个分数层,a[0]=0:表示分数为0分的出现0个人
        int[] bucket = new int[11];
        for(int i = 0; i < a.length; i++) {
            // 出现分数为a[i]的有barrel[a[i]]个人
            bucket[a[i]]++;
        }
        // 打印
        for (int i = 0; i < bucket.length; i++) {
            // 出现几次就打印几次
            for(int j = 1; j <= bucket[i]; j++) {
                System.out.print(i + " ");
            }
        }
    }
}

 

4.复杂度

  • 时间复杂度

在初始化桶时,需要循环n次(n为桶的个数,在java语言中默认都已经初始化为0),在把分数放入桶中时,循环了m次(m为待排序的个数),而在打印时一共循环了(m+n)次,所以整个排序算法一共执行了(n+m+m+n)次。用大写的O来表示时间复杂度,因此该算法的时间复杂度为O(n+m+m+n),即O(2(n+m))。但是一般在说时间复杂度时都是忽略常数,也就是桶排序的最终时间复杂度为O(m+n),并且一般字母都得大写表示,即O(M+N)。

  • 空间复杂度

桶排序所占用的空间比较糟糕,非常浪费空间,如果要排序的数的范围在0~10000000000000,那得创建出10000000000001个变量。创建N个桶,并且待排序的数为M,那么空间复杂度为O(N+M)。

5.优缺点

  • 优点

速度是比较快的,从上面的时间复杂度可以看出。

  • 缺点

比较浪费空间,假设待排序的数中有一个元素值为2100000000000,那么必须创建一个大于2100000000000的桶数。

6.思考

  • 该算法其实可以来做去重复元素,只需要在打印时做一点改动。
// print (de-emphasis) 
for ( int I = 0; I <barrel.length; I ++ ) {
     IF (! Barrel [I] = 0 ) {
         // print is a fraction 
        System.out.print (i + "" ); 
    } 
}

 

  • Currently using bucket sort to sort of score, so if the name and the current score, requested that the names by scores of small to large print output, the current simple bucket sort can not, can only be a fraction of the output.

  1. Reference blog: HTTPS: //www.jianshu.com/p/204ed43aec0c ↩︎

  2. Reference books: "Aha! Algorithm " ↩︎

Guess you like

Origin www.cnblogs.com/flunggg/p/12184657.html