Sorting algorithm --- counting sorting (java version)

Count sort

principle

Counting Sort uses an additional array C, where the i-th element is the number of elements in the array A to be sorted with a value equal to i. Then according to the array C to arrange the elements in A to the correct position. In fact, counting sorting is actually a special case of bucket sorting.

The principle of counting and sorting

  1. Create an array C to find the largest and smallest elements in the array to be sorted;
  2. Count the number of occurrences of each element with the value i in the array and store it in the i-th item of the array C;
  3. Accumulate all counts (starting from the first element in C, each item is added to the previous item);
  4. Backfill the target array: Put each element i in the C(i) item of the new array, and subtract 1 from C(i) for each element.

Code

public class CountSort {
    
    
    public static void main(String[] args) {
    
    
        int[] array = {
    
    5,2,2,6,9,9,3,3,4};
        countSort(array);
        System.out.println(Arrays.toString(array));
    }
    public static void countSort(int[] array) {
    
    
        //求出待排序数组中的最大值和最小值,找出取值区间
        int max = array[0];
        int min = array[0];
        for (int i = 0; i < array.length; i++) {
    
    
            if (array[i] > max) {
    
    
                max = array[i];
            }
            if (min < array[i]) {
    
    
                min = array[i];
            }
        }
        //定义一个额外的数组C
        int bucketSize = max - min + 1;
        int[] bucket = new int[bucketSize];
        //统计对应元素的个数,数组的下标也对应着元素值
        for (int i = 0; i < array.length; i++) {
    
    
            int bucketIndex = array[i] - min;
            bucket[bucketIndex] += 0;
        }
        //对数组C内元素进行累加
        for (int i = 1; i < bucket.length; i++) {
    
    
            bucket[i] = bucket[i] + bucket[i - 1];
        }
        //创建临时数组R 存储最终有序IDE数据列表
        int[] temp = new int[array.length];
        //逆序扫描排序数组  保证元素的稳定性
        for (int i = array.length-1; i >=0; i--) {
    
    
            int bucketIndex = array[i] - min;
            temp[bucket[bucketIndex] - 1] = array[i];
            bucket[bucketIndex] -= 1;
        }

        for (int i = 0; i < temp.length; i++) {
    
    
            array[i] = temp[i];
        }

    }
}

1: What is the time complexity of counting and sorting?

Through the implementation of the code, we found that counting sorting does not involve the comparison of elements, and does not involve the sorting of the elements in the bucket (array C), only the array to be sorted and the traversal operation for counting the array, so the time complexity of counting sorting is O( n+k), where k is the number of buckets, that is, the range of data to be sorted, which is a linear sorting algorithm. Counting sorting is not a comparison sorting, the sorting speed is faster than any comparison sorting algorithm. Since the length k of the array C used for counting depends on the range of the data in the array to be sorted (equal to the difference between the maximum value and the minimum value of the array to be sorted plus 1), this makes counting sorting for arrays with a large range of data required A lot of time and memory.

2: What is the space complexity of counting and sorting?

In the process of counting and sorting, it is necessary to create additional bucket space (array C) to count. Therefore, we can know that the complexity of counting and sorting is: O(n+K), where n is the size of the data and K is the count The number of buckets needed in sorting is actually the length of the array C used for counting. We mentioned earlier that it depends on the range of the data in the array to be sorted.

3: Is counting sorting a stable sorting algorithm?

In the core operation of counting and sorting, we scan the array to be sorted in reverse order, so that the elements in the array to be sorted with the same value but at a later position maintain the same positional relationship in the final sorted array, so the counting is sorted Is a stable sorting algorithm.

4: Applicable scenarios for counting and sorting?

Count sorting can only be used in scenarios where the data range is not large. If the data range k is much larger than the data n to be sorted, counting sorting is not suitable. Moreover, counting sorting can only sort non-negative integers. If the data to be sorted is of other types, it must be converted into non-negative integers without changing the relative size.

Guess you like

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