0

I have written a java sorting algorithm by insertion, the code compiles but doesnt sort:(. If anyone could point out any flaws, Id be very thankful, Java doesnt...

public class Sort {

public static int[] sort(int[] x) {
    int[] y = new int[x.length];
    for(int i = 0; i < x.length; ++i) {
        int j = 0;
        while (y[j] < x[i] && j < i) ++j;
        for (int k = i-1; k >= j; --k) y[k+1] = y[k];
        y[j]=x[i];
    }   
    return y;
}

public static void main(String[] args) {
    int[] size = new int[10];
    for(int k=0; k<size.length; ++k ) {
        size[k]=(int)(Math.random()*20);
        System.out.println(size[k]);
    }
    System.out.println(sort(size));
}}
3
  • Thats the output on the console btw: 7 11 15 12 17 8 1 8 17 2 [I@39ed3c8d so the unsorted Array and then just random... Commented Aug 28, 2020 at 14:47
  • For starters, "while (y[j] < x[i]" references y while y has not been loaded. Your "find insertion point" code should default to 0. The order of boolean expressions protects against things like this, like saying "x != null && x.y > 0" protects x.y from a null x reference. Commented Aug 28, 2020 at 15:34
  • Everyone is answering about your sysout, but those aren't real solutions. I would recommend just using Arrays.toString() to pretty print your array. docs.oracle.com/javase/7/docs/api/java/util/… Commented Aug 28, 2020 at 17:23

3 Answers 3

2

[I@39ed3c8d is returned by invoking toString() on an int array, which you then print with System.out.println.

You presumably want System.out.println(Arrays.toString(sort(size))); instead.

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

Comments

0

You are printing the reference of size i.e., [I@39ed3c8d .

This should help you to understand:

public class Sort {

public static int[] sort(int[] x) {
    int[] y = new int[x.length];
    for(int i = 0; i < x.length; ++i) {
        int j = 0;
        while (y[j] < x[i] && j < i) ++j;
        for (int k = i-1; k >= j; --k) y[k+1] = y[k];
        y[j]=x[i];
    }   
    return y;
}

public static void main(String[] args) {
    int[] size = new int[10];
    System.out.println("Befor sorting");
    for(int k=0; k<size.length; ++k ) {
        size[k]=(int)(Math.random()*20);
        System.out.print(size[k]);
        System.out.print(" ");
    }
    size = sort(size);

    System.out.println("\nAfter sorting");
    for(int i = 0; i<size.length; i++){
        System.out.print(size[i]);
        System.out.print(" ");
    }
}}

Comments

0

That random is result of: System.out.println(sort(size));

Do this instead:

size = sort(size);
   for(int k=0; k<size.length; ++k ) {
    
    

 System.out.print(size[k] + " " );
}

1 Comment

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.