[英]How to optimize this code(Counting Sort)
I was trying to solve classic count sort problem. 我正在尝试解决经典的计数排序问题。 my o/p is right, but the time limit exceeded.How can I optimize the below code.
我的输出是正确的,但是超过了时间限制。如何优化以下代码。 I'm under memory limit.
我的内存有限。
Time Limit: 5 sec Source Limit: 50000 Bytes 时间限制:5秒源限制:50000字节
class TurboStart {
static int integerArray[] = new int[1000001];
public static void main(String[] args) throws NumberFormatException,
IOException {
int i, j;
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
j = Integer.parseInt(reader.readLine());
while (j-- > 0) {
integerArray[Integer.parseInt(reader.readLine())]++;
}
for (i = 0; i < 1000001; i++) {
while (integerArray[i]-- > 0) {
System.out.println(i);
}
}
}
}
Don't use System.out.println
but some smarter way of writing the output( BufferedWriter ?). 不要使用
System.out.println
而是使用一些更聪明的方式来编写输出( BufferedWriter ?)。 Your code for the sort is good so the bottleneck should be the I/O(which is often problem with Java on programming competitions). 您的代码排序很好,因此瓶颈应该是I / O(这在Java编程竞赛中通常是有问题的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.