0

So I have a little code to compare sorting methods(insertion,merge, etc)

I create a random array of objects where I can specify the size of it. I also have a simple loop to show me the array when created and after it's sorted but if the array size is bigger than 955 elements, it does nothing. What is causing this? I gives me no error. It simply "skips" the printing loop.

Loop in question(I have used a for each version of it too with the same result):

for (int x = 0; x < things.length; x++) {
    System.out.print(things[x] + " ");
}
System.out.println();

full code:

public class Root {

    private Thing[] things;
    private int size = 956;

    public Root() {
        super();
    }

    public static void main(String[] args) {
        Root root = new Root();
        root.go();

    }

    private void go() {
        things = new Thing[size];
        long startTime = System.nanoTime();
        for (int x = 0; x < size; x++) {
            things[x] = new Thing();
        }
        long endTime = System.nanoTime();
        System.out.println("Took " + (endTime - startTime) + " nanoseconds to create the Array of Things");

        for (int x = 0; x < things.length; x++) {
            System.out.print(things[x] + " ");
        }
        System.out.println();

        startTime = System.nanoTime();
        SelectionSort.Sort(things);
        // InsertionSort.Sort(things);
        // BubbleSort.SortArray(things);
        // QuickSort.quickSort(things);
        // MergeSorter.SortArray(things);
        endTime = System.nanoTime();
        for (int x = 0; x < things.length; x++) {
            System.out.print(things[x] + " ");
        }
        System.out.println();
        System.out.println("Took " + (endTime - startTime) + " nanoseconds to sort");
    }
}
nail fei
  • 2,179
  • 3
  • 16
  • 36
pcmoreno
  • 101
  • 2
  • 5
  • 4
    Are you using eclipse? Have you tried running this purely through command-line? My guess is you're overflowing a print buffer. – Rogue Nov 20 '16 at 13:55
  • The program seems frozen, maybe because you have a blocking thread in `SelectionSort.Sort(things)` call . It is the single thing which could be the cause of your problem in this code. Try without it to see. And can you show `SelectionSort.Sort()` implementation ? – davidxxx Nov 20 '16 at 13:56
  • Try printing each element on a new line, it fixed it for me. EDIT: Actually, with your code, for some reason the text is in the console, but it is white instead of black. – Tefek Nov 20 '16 at 14:17
  • @Rogue is right, that's the most likely problem (and it's a common one in Eclipse with people who don't use newlines) – Erwin Bolwidt Nov 20 '16 at 14:39
  • Based on what Rogue said: Right-click into the Eclipse console, remove the checkmark at "Limit Console Output" and try again. **If this does not help:** Print the result in individual lines. Very long lines are often problematic (even in sophisticated text editors!) – Marco13 Nov 20 '16 at 14:56
  • Rogue , yes I am using Eclipse. 493msi , indeed, it's text is in white. Odd that it switches to white on white for that. @Marco13 , That solves the problem. I changed the buffer limit and it's fine now. davidxxx It is not blocking, the program runs totally. Just the printing on console that was messed up. Thank you all for the help! – pcmoreno Nov 20 '16 at 18:31

0 Answers0