I'm faced with the problem of sorting a sequence of 32-bit signed integers (-2,147,483,648 to 2,147,483, 647) with input size n from 0 to 1m. The input numbers are separated by a new line and the first number in std in represents the number of inputs / numbers. I first read in the input using a buffered input reader
BufferedReader bufferedInput = new BufferedReader(new InputStreamReader(System.in));
int NOI = 0; // Number of inputs
try{
NOI = Integer.parseInt(bufferedInput.readLine());
}
catch(IOException e) {
}
int[] a = new int[NOI];
try{
for(int i = 0; i < NOI; i++){
a[i] = Integer.parseInt(bufferedInput.readLine());
}
}
catch(IOException e){
}
then I preform a radix sort (in place) on my array a, I then proceed to print my sorted array (ascending) to the std output with each number separated by a new line.
for(int i = 0; i < NOI; i++){
System.out.println(Integer.toString(a[i]));
}
This is an optimization problem, I tested my code on ideone.com using input from appincredible.com/online/random-number-generator/ and found that for an input size of 1000 numbers my run times to 3sf for each step were as follows:
Read input - 14.5 ms
Sorting - 1.28 ms
Printing Output - 56.2 ms
I'm looking to make my solution to this problem more efficient and from the little bit of testing I've done I've noticed that the majority of runtime is on the output phase. How can I then, iterate through my array printing each element to a new line in std out more efficiently? Looking at What is the most efficient way to convert an int to a String? and Most efficient way of converting String to Integer in java suggest that the Integer.toString() is the most efficient for converting my integers to a string for output.
Any pointers? This is my first question here and I would love some help, I've looked around and can't find much like it. Possibly my issue lies in the data structure I use to store my array? I'm not too sure Thanks :)