-4

我编写了这段代码,用于使用计数排序对数组元素进行排序。该程序编译并运行,但没有给出正确的输出。我希望元素以非降序排序。我得到的输出按非降序排序,但值与我输入的不同。我已经检查了很多次代码,但我无法发现错误。请帮忙。

import java.io.*;

public class Main {
public static void main(String aa[]) {

    try {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

        int t = Integer.parseInt(input.readLine());
        int a [] = new int[t];
        int c [] = new int[t];
        int max = 0;

        if(t<=1000000) {

            for(int i = 0; i<t; i++) {
                int n = Integer.parseInt(input.readLine());
                if(n>=0 && n<=1000000) {
                    a[i] = n;
                    if(n>max) max = n;
                }
            }

            int b[] = new int[max+1];

            for(int i = 0; i<max+1; i++)
                b[i] = 0;

            for(int i = 0; i<t; i++)
                b[a[i]] += 1;

            for(int i = 1; i<max+1; i++)
                b[i] += b[i-1];

            for (int i = t-1; i>=0; i--) {
                c[b[a[i]]] = a[i];
                b[a[i]]--;
            }

            for (int i = 0; i<t; i++) 
                System.out.println(c[i]);

        }
        else 
            System.exit(0);
    } catch (Exception e) {System.out.println(e);}
  }
}
4

3 回答 3

3

关于你错的地方你是对的。改变:

for (int i = t-1; i>=0; i--) {
     c[b[a[i]]] = a[i];
     b[a[i]]--;
}

进入:

for (int i = 0; i<t; ++i) {
     c[b[a[i]]-1] = a[i];
     --b[a[i]];
}

它应该适用于您的测试数据。

于 2013-08-25T16:43:23.957 回答
2

你几乎做对了。更改以下行(由一个错误关闭)

for (int i = 0; i < t; i++) {
    c[b[a[i]] - 1] = a[i];
    b[a[i]]--;
}
于 2013-08-25T16:46:06.550 回答
0

请使用它来计数排序:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

/**
 * Counting sort.
 */
public class CountingSort {

    static int[] countingSort(int[] arr) {
        int arrMax = Arrays.stream(arr).max().getAsInt();
        int[] occurances = new int[arrMax + 1];
        Arrays.stream(arr).forEach(n -> {
            occurances[n]++;
        });
        int[] result = new int[arr.length];
        int counter = 0;
        for (int a = 0; a < occurances.length; a++) {
            if (occurances[a] > 0) {
                for (int b = 1; b <= occurances[a]; b++) {
                    result[counter] = a;
                    counter++;
                }
            }
        }
        return result;
    }

    public static void main(String[] args) throws IOException {
        try (BufferedWriter bufferedWriter =
                     new BufferedWriter(new FileWriter(new File("iofiles/cs.out")));
             Scanner scanner = new Scanner(System.in)) {
            int n = scanner.nextInt();
            scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
            int[] arr = new int[n];
            String[] arrItems = scanner.nextLine().split(" ");
            scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
            for (int i = 0; i < n; i++) {
                int arrItem = Integer.parseInt(arrItems[i]);
                arr[i] = arrItem;
            }
            int[] result = countingSort(arr);
            for (int i = 0; i < result.length; i++) {
                bufferedWriter.write(String.valueOf(result[i]));
                if (i != result.length - 1) {
                    bufferedWriter.write(" ");
                }
            }
            bufferedWriter.newLine();
        }
    }
}

样本输入:

100
63 25 73 1 98 73 56 84 86 57 16 83 8 25 81 56 9 53 98 67 99 12 83 89 80 91 39 86 76 85 74 39 25 90 59 10 94 32 774 3 86 960 2 729 3 86 960 2 21 92 69 81 40 40 34 68 78 24 87 42 69 23 41 78 22 6 90 99 89 50 30 20 1 43 3 70 95 33 46 44 9 69 48 33 60 65 16 82 79 77 61 75 1 2 2 33

样本输出:

1 1 3 3 6 8 9 9 10 12 13 16 16 18 20 21 21 22 23 24 25 25 25 27 27 30 30 32 32 32 33 33 33 34 39 39 40 40 41 42 43 45 4 4 35 46 45 64 44 46 56 6 57 59 60 61 63 65 67 67 68 69 69 69 70 70 73 73 74 75 75 76 78 78 79 79 80 81 81 82 83 83 84 85 86 86 87 87 89 89 89 90 90 91 92 94 95 96 98 98 99 99

具体来说,这个方法是做计数排序:

static int[] countingSort(int[] arr) {
        int arrMax = Arrays.stream(arr).max().getAsInt();
        int[] occurances = new int[arrMax + 1];
        Arrays.stream(arr).forEach(n -> {
            occurances[n]++;
        });
        int[] result = new int[arr.length];
        int counter = 0;
        for (int a = 0; a < occurances.length; a++) {
            if (occurances[a] > 0) {
                for (int b = 1; b <= occurances[a]; b++) {
                    result[counter] = a;
                    counter++;
                }
            }
        }
        return result;
    }
于 2018-09-15T16:38:01.663 回答