Common sorting algorithm - bucket sort

Bucket sort is a non-comparative sorting algorithm. Its basic idea is to partition an array into a finite number of buckets. Each bucket is sorted individually (it is possible to use another sorting algorithm or continue to use bucket sorting recursively for sorting). Finally, take out the elements in each bucket in turn to get an ordered array. Bucket sort is an inductive result of pigeonhole sort. Bucket sorting is suitable for situations where the data is evenly distributed and the time complexity is linear.

The process of bucket sorting:

1 First you need to determine the maximum and minimum values ​​of the array, and then determine the number of buckets based on the maximum and minimum values ​​of the array.
2 Put each element in the array into the corresponding bucket.
Sort each bucket.
3. Take out the elements in each bucket in order to form the final ordered array.

The c language implementation is as follows:

void bucket_sort(int arr[], int n) {
    
    
    int i, j;
    int count[n];
    for (i = 0; i < n; i++) {
    
    
        count[i] = 0;
    }
    for (i = 0; i < n; i++) {
    
    
        (count[arr[i]])++;
    }
    for (i = 0, j = 0; i < n; i++) {
    
    
        for (; count[i] > 0; (count[i])--) {
    
    
            arr[j++] = i;
        }
    }
}

The time complexity of bucket sorting is O(n), and the space complexity is O(n). Bucket sorting is a very efficient sorting algorithm. Its time complexity is linear, and it is more suitable for even data distribution. .

The advantage of bucket sorting is that the sorting speed is fast, the time complexity is O(n), and it is a stable sorting algorithm.

However, the disadvantage of bucket sorting is that it requires additional space, because several buckets need to be opened to store data.

Bucket sorting is suitable for even data distribution. If the data distribution is uneven, the number of buckets will be very large and the space overhead will be large.

The application scenarios of bucket sorting are: counting sorting and cardinality sorting. Bucket sorting is an efficient sorting algorithm with a linear time complexity, suitable for even data distribution, but requires additional space.

It should be noted that the efficiency of bucket sorting depends on the sorting algorithm inside the bucket. If you use quick sort or merge sort inside the bucket, the efficiency will be very high. If you use simple sorting algorithms, such as insertion sort or bubble sort, the efficiency will become very low.

In addition, bucket sorting also has high requirements for data. It is necessary to ensure that the data is random and the data distribution must be uniform. Otherwise, the number of buckets will be very large, wasting space, and there is no advantage.

In short, bucket sorting is an efficient sorting algorithm, but its applicable scenarios are very limited. When the data is random and evenly distributed, bucket sorting can give full play to its advantages.

Guess you like

Origin blog.csdn.net/sorcererr/article/details/128700770
Recommended