Reputation: 130
I have a very simple program that times how long it takes to selection-sort 100000 numbers. Here's the code:
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Stopwatch {
public static void main(String[] args) {
// Create a stopwatch
int howManyNumbers = 100000;
Integer[] numbers = new Integer[howManyNumbers];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = (int)(100 * Math.random());
}
System.out.println("Array created");
StopWatching watch = new StopWatching();
System.out.println("Sorting started");
for (int i = 0; i < numbers.length; i++ ) {
for (int j = i; j < numbers.length; j++) {
if (numbers[j] < numbers[i]) {
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
System.out.print("Finished the sort");
watch.stop();
System.out.println();
System.out.println(watch.getElapsedTime());
for (int i = 0; i < numbers.length; i++) {
System.out.print(numbers[i]);
}
}
}
This runs, prints "Array Created" and "Sorting Started" to the console, goes for a second or two, then just stops. No error or warning. Doesn't print "Finished the sort" or anything like that. It is supposed to print "Finished the sort", then the time elapsed which is returned from the watch.getElapsedTime() (object of StopWatching class, which works, has been tested in other environments).
Can anyone indicate what I'm doing wrong here?
NOTE: The return type of watch.getElapsedTime() is a Long which is the time in milliseconds calculated from when the watch is created to when the watch.stop() method is called (by utilizing the GregorianCalendar class).
P.S. I'm using eclipse IDE
Upvotes: 0
Views: 3268
Reputation: 32980
The problem is the GUI console output of Eclipse.
The code
for (int i = 0; i < numbers.length; i++) {
System.out.print(numbers[i]);
}
prints 100000 numbers in one line.
This is no problem when running with java
in an OS console.
But when done in Eclipse the console just shuts down an removes any previous print out.
This was my first attempt to analyze:
I guess that the program didn't stop but is still running. The inner of the loop is executed 100000*100000 times.
In Eclipse use the debug view to see if a programm has stopped or is still running.
Upvotes: 5
Reputation: 27812
The program runs fine. There is just too much output for the Eclipse console to display it. If you run it from the command line and redirect the output to a file > output.txt, the would output will be displayed correctly.
You can also see part of the output in Eclipse replacing System.out.println with a StringBuilder appending the whole output like below
StringBuilder sb = new StringBuilder(2048);
sb.append("Array created\n");
sb.append("Sorting started");
for (int i = 0; i < numbers.length; i++) {
for (int j = i; j < numbers.length; j++) {
if (numbers[j] < numbers[i]) {
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
sb.append("Finished the sort\n");
for (int i = 0; i < numbers.length; i++) {
sb.append(numbers[i]);
sb.append(", ");
}
System.out.println(sb);
Alternatively in Eclipse console, right click, Properties, Check box Limit Console Output to uncheck (checked by default) and all of your output will be displayed in Eclipse.
Upvotes: 3
Reputation: 109557
At times print a newline (here println). This flushes the output buffer.
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
One remark: best would be to use the primitive type int:
int[] numbers = new int[howManyNumbers];
Integer is a wrapper class for int values. Otherwise use:
Integer temp = ...
Momentarily Integer/int conversions take place.
Upvotes: 2