0

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

6
  • 1
    We can't reproduce the problem without the StopWatching class. Commented Nov 27, 2015 at 11:54
  • works just fine (though i had to remove the calls for the Stopwatch). Probably there's some issue with the implementation of Stopwatching, or this is just a bug of eclipse Commented Nov 27, 2015 at 11:54
  • @user3340140 RealSkeptic has a point, it that StopWatching in same package, do you need to import? Is it from a library? Commented Nov 27, 2015 at 11:58
  • I tested it - you need patience - as it took allmost 5 seconds to complete. Commented Nov 27, 2015 at 12:00
  • 1
    just a sidenote, but this isn't selection-sort. That code uses bubble-sort Commented Nov 27, 2015 at 12:00

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

the console, at the top says "Terminated", which shows only after the program stops.
@wero tested it, the program terminates
@wero i ran it from the console. terminate within seconds.
@Paul your box is definitely faster than mine :-)
3

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.

enter image description here

Comments

2

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.

3 Comments

Adding in if (i % 10 == 0) { System.out.println(); } Did infact work. But I don't know why. Care to shed some light? I should note, commenting out the printing of all the numbers also allowed the rest of the program to work. Is this overflowing the print buffer or something?
@RealSkeptic you are right for 100_000², that might take 3 years or such.
The System.out is buffered. Now in the IDE this is intercepted with setOut, and in the operating system the case is different again. When the buffer overflows, the full buffer should be flushed, and then the overflowed text written. The operating system has its own buffer and might have a similar hick-up, or might be needing larger allocation. The costs are hard to tell. Linux/Mac probably are faster than Windows. In any case you will see first console output later. How much depends on how restricted the buffer sizes are. And that is not good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.