【问题标题】:Need help to understand an implementation of the Counting Sort sort algorithm需要帮助来理解计数排序排序算法的实现
【发布时间】:2021-03-22 05:39:30
【问题描述】:

这段代码是关于算法和数据结构的。这段代码运行完美,我只是有一些问题,因为我似乎不明白两点。所以我的问题是:

  1. countingArray 中有哪些信息?
  2. while 循环多久执行一次?

public class CountingSort {

    public static void main(String[] args) {
        int[] m1 = { 1, 17, 3, 1, 4, 9, 4, 4 };

        System.out.println("unsorted:");
        output(m1);
        int min1 = rangeMin(m1);
        int max1 = rangeMax(m1);

        countingSort(m1, min1, max1);
        System.out.println("sorted:");
        output(m1);

        int[] m2 = { -1, 13, 3, -1, -4, 9, -4, 4 };
        System.out.println("unsorted:");
        output(m2);
        int min2 = rangeMin(m2);
        int max2 = rangeMax(m2);

        countingSort(m2, min2, max2);
        System.out.println("sorted:");
        output(m2);
    }

    public static void output(int[] a) {
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + ", ");
        }
        System.out.println();
    }

    public static int rangeMin(int[] a) {
        int minimum = a[0];
        for (int i = 1; i < a.length; i++) {
            if (a[i] < minimum)
                minimum = a[i];
        }
        return minimum;
    }

    public static int rangeMax(int[] array) {
        int maximum = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] > maximum)
                maximum = array[i];
        }
        return maximum;
    }

    public static void countingSort(int[] array, int rangeMin, int rangeMax) {
        int[] countingArray = new int[rangeMax - rangeMin + 1];
        for (int i : array) {
            countingArray[i - rangeMin]++;
        }
        int c = 0;

        for (int i = rangeMin; i <= rangeMax; i++) {
            while (countingArray[i - rangeMin] > 0) {

                array[c] = i;
                c++;
                countingArray[i - rangeMin]--;
            }
        }
    }
}

【问题讨论】:

  • 这个数组只是保存了你看到每个元素的次数。对于每个可能的元素,您可以让 while 循环遍历该元素的计数。所以while循环运行的次数与所有计数的总和一样多,也就是输入的长度。

标签: java algorithm sorting data-structures counting-sort


【解决方案1】:

CountingSort 具有 O(n) 时间和空间复杂度。你迭代(即使用for loop)两次。

public class CountingSort {

    public static void main(String... args) {
        proceed(1, 17, 3, 1, 4, 9, 4, 4);
        System.out.println("---");
        proceed(-1, 13, 3, -1, -4, 9, -4, 4);
    }

    public static void proceed(int... arr) {
        System.out.print("unsorted: ");
        print(arr);

        countingSort(arr);

        System.out.print("sorted:   ");
        print(arr);
    }

    public static void print(int... arr) {
        System.out.println(Arrays.stream(arr)
                                 .mapToObj(i -> String.format("%2d", i))
                                 .collect(Collectors.joining(",")));
    }

    public static void countingSort(int... arr) {
        int min = Arrays.stream(arr).min().orElse(0);
        int max = Arrays.stream(arr).max().orElse(0);
        // contains amount of number in the unsorted array
        // count[0] - amount of min numbers
        // count[count.length - 1] - amount of max numbers
        int[] count = new int[max - min + 1];

        for (int i : arr)
            count[i - min]++;

        // fill source array with amount of numbers
        for (int i = 0, j = 0; i < count.length; i++)
            for (int k = 0; k < count[i]; k++, j++)
                arr[j] = min + i;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 1970-01-01
    • 2011-05-10
    相关资源
    最近更新 更多