Question

I am taking a Java class at school. Java looks simple but complicated for me.

I am having a trouble with sorting a two-dimensional array. Please Please Please help me.

Here is what I coded. I don't know why it doesn't work.

public static void selectionSort(int[][] list) {
    for (int k = 0; k < list.length;k++){
        for (int i = 0; i < list[k].length;i++) {
            int currentMin = list[k][i];
            int currentMinIndexRow = k;
            int currentMinIndexColumn = i;

            if (k == 3 && i == 3) continue;

            for (int m = k; m < list.length; m++) {
                for (int j = i; j < list[k].length; j++) {
                    if (m == k && i == j) continue;

                    if (currentMin > list[m][j]) {
                        currentMin = list[m][j];
                        currentMinIndexRow = m;
                        currentMinIndexColumn = j;
                    }
                }
            }        
            if (currentMinIndexRow != k && currentMinIndexColumn != i) {
                list[currentMinIndexRow][currentMinIndexColumn] = list[k][i];
                list[k][i] = currentMin;
            }
        }
    }
}

Thank you so much guys!!!!!


I am trying to write a program that prompts the user to enter two lists of integers and displays whether the two are identical.

Such as, "Enter list1: 51 25 22 6 1 4 24 54 6 Enter list2: 51 22 25 6 1 4 24 54 6 The two arrays are identical

Enter list1: 51 5 22 6 1 4 24 54 6 Enter list2: 51 22 25 6 1 4 24 54 6 The two arrays are not identical

Here is what I wrote.

public static void main(String[] args) {
    java.util.Scanner input = new java.util.Scanner(System.in);
    int[][] list1 = new int[3][3];
    int[][] list2 = new int[3][3];

    System.out.print("Enter list1: ");
    for (int row=0 ;row < list1.length ;row++){
        for (int column=0;column<list1[row].length; column++){
            list1[row][column] = input.nextInt();
        }
    }
    System.out.print("Enter list2: ");
    for (int row=0 ;row < list2.length ;row++){
        for (int column=0;column<list2[row].length; column++){
            list2[row][column] = input.nextInt();
        }
    }
    selectionSort(list1);
    selectionSort(list2);

    if (equals(list1, list2) == true) 
        System.out.println("The two arrays are identical");
    else
        System.out.println("The two arrays are not identical");

}// void main

public static boolean equals(int[][] m1, int[][] m2){
    boolean result = true;
    for (int row=0 ;row < m1.length ;row++){
        for (int column=0 ;column<m1[row].length ; column++){
            if (m1[row][column] != m2[row][column]) {result = false; break;}
        }
    }
    return result;
}// boolean equals

public static void selectionSort(int[][] list) {

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

        for(int i = 0; i < list[k].length;i++) {
            int currentMin = list[k][i];
            int currentMinIndexRow = k;
            int currentMinIndexColumn = i;

            if(k == 3 && i == 3) continue;

            for(int m = k; m < list.length; m++){
                for (int j = i; j < list[k].length; j++) {
                    if (m == k && i == j) continue;

                    if (currentMin > list[m][j]) {
                        currentMin = list[m][j];
                        currentMinIndexRow = m;
                        currentMinIndexColumn = j;
                    }
                }
            }       
                    if (currentMinIndexRow != k && currentMinIndexColumn != i) {
                        list[currentMinIndexRow][currentMinIndexColumn] = list[k][i];
                        list[k][i] = currentMin;
                    }


        }
    }
}

Thank you for your comments.

Was it helpful?

Solution

Thank you so much guys!!!!

I figured out why I was wrong.

There was a really really really simple error.

if (currentMinIndexRow != k || currentMinIndexColumn != i) {
                        list[currentMinIndexRow][currentMinIndexColumn] = list[k][i];
                        list[k][i] = currentMin;
                    }

The above code is what I corrected. Thank you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top