Sorting Algorithm — Bucket Sort

Introduction to Bucket Sort

Suppose there are N integers in the array a to be sorted, and the data in the array a is knownRange [0, MAX). When sorting buckets, create a bucket array (here is the array) r with a capacity of MAX, and initialize the elements of the bucket array to 0; treat each unit in the bucket array with a capacity of MAX as a "bucket".

When sorting, the array a is traversed one by one, and the value of the array a is used as the subscript of the "bucket array r". When the data in a is read, add 1 to the value of the bucket. For example, if the array a[3]=5 is read, the value of r[5] will be +1.

bucket sort implementation

Suppose a={8,2,3,4,3,6,6,3,9}, max=10. At this point, put all the data in the array a into the buckets that need to be 0-9. As shown below:

insert image description here

After putting the data into the bucket, through a certain algorithm, the data in the bucket is taken out and converted into an ordered array. We get the result we want.

Bucket Sort Complexity and Stability

Bucket Sort Complexity

  • Average time complexity: O(n + k)
  • Optimal Time Complexity: O(n + k)
  • Worst time complexity: O(n^2)
  • Space complexity: O(n * k)

Bucket sorting uses linear time O(n) in the best case. The time complexity of bucket sorting depends on the time complexity of sorting data between buckets, because the time complexity of other parts is O(n). Obviously, the smaller the buckets are divided, the less data there is between each bucket, and the less time it takes to sort. But the corresponding space consumption will increase.

bucket sort stability

Stability: Stable

Code


package com.zxn;

/**
 * @author zxn
 * @ClassName BucketSort
 * @Description
 * @createTime 2023年05月05日 08:12:00
 */
public class BucketSort {
    /*
     * 桶排序
     *
     * 参数说明:
     *     a -- 待排序数组
     *     max -- 数组a中最大值的范围
     */
    public static void bucketSort(int[] a, int max) {
        int[] buckets;

        if (a==null || max<1)
            return ;

        // 创建一个容量为max的数组buckets,并且将buckets中的所有数据都初始化为0。
        buckets = new int[max];

        // 1. 计数
        for(int i = 0; i < a.length; i++) {
            buckets[a[i]]++;
        }

        // 2. 排序
        for (int i = 0, j = 0; i < max; i++) {
            while( (buckets[i]--) >0 ) {
                a[j++] = i;
            }
        }

        buckets = null;
    }

    public static void main(String[] args) {
        int i;
        int a[] = {8,2,3,4,3,6,6,3,9};

        System.out.printf("before sort:");
        for (i=0; i<a.length; i++) {
            System.out.printf("%d ", a[i]);
         }
        System.out.printf("\n");

        bucketSort(a, 10); // 桶排序

        System.out.printf("after  sort:");
        for (i=0; i<a.length; i++) {
            System.out.printf("%d ", a[i]);
         }
        System.out.printf("\n");
    }
}


Core & Summary

  • It is required that the elements of the sorted array must be all non-negative integers
  • The maximum value of the array is the size of the bucket
  • The value of the array element is the subscript of the bucket
  • Count the number of bucket elements that are array element values

Guess you like

Origin blog.csdn.net/SO_zxn/article/details/130498709