0

In class our teacher asked us to sort a list of arrays and then give it back as a 2D array, with every change printed.

This is the array: 35, 7, 63, 42, 24, 21 and in the end the program should print out like this:

[ 7 35 63 42 24 21 ]

[ 7 21 63 42 24 35 ]

[ 7 21 24 42 63 35 ]

[ 7 21 24 35 63 42 ]

[ 7 21 24 35 42 63 ]

[ 7 21 24 35 42 63 ]

I have following code but somehow it simply does not work (note that public static void is already given so we have to implement public static int[][] selectionsort(int[] a)

What's wrong with it?

public static void main(String[] args) {

    int[] a = new int[] { 35, 7, 63, 42, 24, 21 };
    int[][] c = selectionsort(a);
    for (int i = 0; i < c.length; i++){
        System.out.print("[  ");
        for (int j = 0; j < c[i].length; j++) {
            System.out.print(c[i][j]+"  ");     
        }
        System.out.println("]");
    }
    /*
     * expected printout
     * [  7  35  63  42  24  21  ]
     * [  7  21  63  42  24  35  ]
     * [  7  21  24  42  63  35  ]
     * [  7  21  24  35  63  42  ]
     * [  7  21  24  35  42  63  ]
     * [  7  21  24  35  42  63  ]
     */
}
public static int[][] selectionsort(int[] a) {

    for (int i = 0; i < a.length - 1; i++) {
        for (int j = i + 1; j < a.length; j++) {
            if (a[i] > a[j]) {
                int temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
    }

    return a;
}
4
  • You need to create a 2D array to fill and return in selection sort rather than returning the argument a. Commented Nov 29, 2015 at 17:50
  • Okay I understand that but how do I do this? I'm new and I really have no clue… Commented Nov 29, 2015 at 17:51
  • What your teacher wants is that you create a copy of the array being sorted at each iteration of the sort algorithm, and store that copy as an element of an array or int arrays. When the algorithm is done, return the array of arrays containing all the intermediate copies. So, google for "how to define a 2D array in Java", and "how to create a copy of an array in Java". Commented Nov 29, 2015 at 17:52
  • Selection Sort in Java : pastebin.com/VUd9WHWE Commented Nov 30, 2015 at 5:18

2 Answers 2

1

You have to keep track of the array putting it in the 2D array after each iteration, and return that 2D array

public class Main {

public static void main(String[] args) {

    int[] a = new int[]{35, 7, 63, 42, 24, 21};
    int[][] c = selectionsort(a);
    for (int i = 0; i < c.length; i++) {
        System.out.print("[  ");
        for (int j = 0; j < c[i].length; j++) {
            System.out.print(c[i][j] + "  ");
        }
        System.out.println("]");
    }
/*
 * expected printout
 * [  7  35  63  42  24  21  ]
 * [  7  21  63  42  24  35  ]
 * [  7  21  24  42  63  35  ]
 * [  7  21  24  35  63  42  ]
 * [  7  21  24  35  42  63  ]
 * [  7  21  24  35  42  63  ]
 */
}

public static int[][] sort(int[] a) {
    int result[][] = new int[a.length][a.length];

    for (int i = 0; i < a.length; i++) {
        for (int j = i + 1; j < a.length; j++) {
            if (a[i] > a[j]) {
                int temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }

        for (int k = 0; k < a.length; k++) {
            result[i][k] = a[k];
        }
    }

    return result;
 }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I didn't verify the selection sort logic you implemented, but the problem is given in any standard compiler: Type mismatch: cannot convert from int[] to int[][]

What you have to do: You have to return the history of the selection sort algorithm which is a 2 dimensional array with each row a step in the selection sort algorithm.

What you have done: You returned the solution of the sorting algorithm which is a 1 dimensional array.

1 Comment

How do I return a 2 dimensional array? I am aware that I need to return the history but I don't know the right way to do so.

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.