我编写了这段代码,用于使用计数排序对数组元素进行排序。程序编译并运行但不提供正确的输出。我希望元素按非递减顺序排序。我得到的输出按非递减顺序排序,但值与我输入的值不同。我已多次检查代码,但我无法发现错误。请帮忙。
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);}
}
}
答案 0 :(得分: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]];
}
它应该适用于您的测试数据。
答案 1 :(得分:2)
你几乎做对了。更改以下行(关闭一个错误)
for (int i = 0; i < t; i++) {
c[b[a[i]] - 1] = a[i];
b[a[i]]--;
}
答案 2 :(得分: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 44 3 89 30 27 79 46 96 27 32 18 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 67 61 32 21 79 75 75 13 87 70 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 44 44 46 46 48 50 53 56 56 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;
}