Sorting algorithm-bucket sorting (java version)

Bucket sort

Bucket sort (Bucket Sort) As the name suggests, will be used "bucket", we can think of it as a container, the core idea is to sort the data down into several ordered bucket, each bucket data is then separately Sort. After sorting in the bucket, the data in each bucket is taken out in order, the sequence is ordered, in other words: bucket sorting is to store the elements in the same range in the set to be sorted In the same bucket, that is, if the collection is split into multiple regions based on the element value characteristics, the multiple buckets formed after splitting are in an orderly state from the point of view of the value range. If you sort the elements in each bucket, the set consisting of all the elements in the bucket is sorted.

There are two key links in the bucket sorting process :

  1. The division of the element value range is the mapping rule of the element to the bucket. The mapping rules need to be selected according to the element distribution characteristics of the set to be sorted. If the rule design is too vague and broad, all elements in the set to be sorted may be mapped to one bucket. If the mapping rule is designed to be too specific and strict, It may cause each element value in the set to be sorted to be mapped to a bucket.
  2. From the process of mapping elements in the set to be sorted to each bucket, there is no comparison and exchange operation of the elements. When sorting the elements in each bucket, you can independently select the appropriate sorting algorithm. The sorting algorithm in each bucket is Complexity and stability determine the complexity and stability of the final algorithm.

Bucket sorting is more suitable for non-memory sorting.

The so-called non-memory sorting means that the data is stored in an external disk, the amount of data is relatively large, the memory is limited, and it is impossible to load all the data into the memory. In addition, it can be seen from the bucket sorting process that when there is a large difference in the value of the elements in the set to be sorted, the selection of the mapping rule is a challenge, and sometimes it may cause the elements to be concentrated in a certain bucket or most of the buckets are empty. This phenomenon has a greater impact on the time complexity or space complexity of the algorithm, so bucket sorting is suitable for sequences with relatively concentrated element value distribution, or the elements to be sorted can be evenly distributed in a certain range [MIN, MAX] between.

The specific description of the bucket sorting algorithm:

  • Artificially set a BucketSize as how many different values ​​can be placed in each bucket (for example, when BucketSize==5, the bucket can store {1,2,3,4,5} these types of numbers, but the capacity is not limited. That can store 100 3);
  • Traverse the input data, and put the data one by one into the corresponding bucket;
  • To sort each bucket that is not empty, other sorting methods can be used, or bucket sorting can be used recursively;
  • Concatenate sorted data from buckets that are never empty.

Note that if you use bucket sorting to sort each bucket recursively, when the number of buckets is 1, you must manually reduce the BucketSize and increase the number of buckets in the next cycle, otherwise it will fall into an infinite loop and cause memory overflow.

Code

public class BucketSort {
    
    
    public static void main(String[] args) {
    
    
        List<Integer> list = new ArrayList<>();
        list.add(5);
        list.add(2);
        list.add(2);
        list.add(6);
        list.add(9);
        list.add(0);
        list.add(3);
        list.add(4);
        List<Integer> bucketSort = bucketSort(list, 2);
        System.out.println(bucketSort);

    }
    public static List<Integer> bucketSort(List<Integer> array,int bucketSize) {
    
    
        //合法性校验
        if (array == null || array.size() < 2 || bucketSize < 1) {
    
    
            return array;
        }
        //找出集合中元素的最大值和最小值
        int max = array.get(0);
        int min = array.get(0);
        for (int i = 0; i < array.size(); i++) {
    
    
            if (array.get(i) > max) {
    
    
                max = array.get(i);
            }
            if (array.get(i) < min) {
    
    
                min = array.get(i);
            }
        }
        //计算桶的个数   集合中的最小值到最大值 判断桶的个数
        int bucketCount = (max - min) / bucketSize + 1;
        //按照顺序创建桶 创建一个list带下标
        List<List<Integer>> bucketList = new ArrayList<>();
        for (int i = 0; i < bucketCount; i++) {
    
    
            bucketList.add(new ArrayList<Integer>());
        }
        //将待排序的集合依次添加到对应的桶中
        //首先找到元素所对应的桶的顺序  再将元素添加到桶中
        for (int j = 0; j < array.size(); j++) {
    
    
            int bucketIndex = (array.get(j) - min) / bucketSize;
            bucketList.get(bucketIndex).add(array.get(j));
        }

        //将桶内的元素进行排序
        List<Integer> resultList = new ArrayList<>();
        for (int j = 0; j < bucketList.size(); j++) {
    
    
            List<Integer> everyBucket = bucketList.get(j);
            if (everyBucket.size() >0) {
    
    
                if (bucketCount == 1) {
    
    
                    bucketSize--;
                }
                //递归调用  进行排序
                List<Integer> temp = bucketSort(everyBucket,bucketSize);
                for (int i = 0; i < temp.size(); i++) {
    
    
                    //合并数据
                    resultList.add(temp.get(i));
                }
            }
        }
        return resultList;
    }
}

1: What is the time complexity of bucket sorting?

The time complexity of bucket sorting depends on the time complexity of sorting the data between each bucket. If we map the elements to be sorted to a certain bucket mapping rule well, obviously, the smaller the buckets are divided , The less data between each bucket, the less time it takes to sort. But the corresponding space consumption will increase. We generally use fast sorting when sorting the elements in each bucket or recursive bucket sorting. Through our initial analysis, when we use fast sorting for each bucket, if the number of buckets is close to the data size n, The complexity is O(n), if in extreme cases the complexity degenerates to O(n* log n).

2: What is the space complexity of bucket sorting?

Because you need to apply for additional space to store elements and apply for additional space to store each bucket, the space complexity is O(N+M), where M represents the number of buckets. So although bucket sorting is fast, it uses space for time.

3: Is bucket sorting a stable sorting algorithm?

Whether bucket sorting is stable depends on the stability of the algorithm for sorting the elements in each bucket. If we use fast sorting for the elements in the bucket, bucket sorting is an unstable sorting algorithm.

Guess you like

Origin blog.csdn.net/qq_33626996/article/details/113175358