简体   繁体   中英

Sorting Two-Dimensional Array by Row

The requirement is to sort the rows of a two-dimensional array. I feel like my code is very close to being done, but I can't figure out why it isn't displaying the sorted array. I forgot to mention that we are not allowed to use the premade sorting methods. The problem is most likely in the sortRows method. Anyways, here's my code:

public class RowSorting
{
   public static void main(String[] args) 
{
  double[][] numbers  = new double[3][3];
  double[][] number  = new double[3][3];
  int run = 0;
  String answer = "";

  while (run == 0)
     {
       Scanner input = new Scanner(System.in);
       System.out.print("Enter a 3-by-3 matrix row by row: ");
       for(int row = 0; row < numbers.length; row++)
        {
         for(int column = 0; column < numbers[row].length; column++)
          {
           numbers[row][column] = input.nextDouble();
          }
        }
       for(int row = 0; row < numbers.length; row++)
        {
         for(int column = 0; column < numbers[row].length; column++)
          {
           System.out.print(numbers[row][column] + " ");
          }
         System.out.print("\n");
        } 
       System.out.println("The sorted array is: \n");
       number = sortRows(numbers);
       for(int row = 0; row < number.length; row++)
        {
         for(int column = 0; column < number[row].length; column++)
          {
           System.out.print(number[row][column] + " ");
          }
         System.out.print("\n");
        } 




   System.out.print("\nWould you like to continue the program (y for yes or    anything else exits): ");
       answer = input.next();

       if(answer.equals("y"))
        {
         continue;
        }
       else
         break;
      }




}
 public static double[][] sortRows(double[][] m)
{
  for(int j = 0; j < m[j].length - 1; j++)
   {
    for(int i = 0; i < m.length; i++)
    {
      double currentMin = m[j][i];
      int currentMinIndex = i;

      for(int k = i + 1; k < m[j].length; k++)
      {
       if(currentMin > m[j][i])
       {
        currentMin = m[j][i];
        currentMinIndex = k;
       }
      }
    if(currentMinIndex != i)
    {
     m[currentMinIndex][j] = m[j][i];
     m[j][i] = currentMin;
    }
    }
   }
  return m;
 }
}

It looks like this block:

if(currentMin > m[j][i])
   {
    currentMin = m[j][i];
    currentMinIndex = k;
   }

Will never happen. Because you just assigned currentMin to m[j][i] two lines before it. I believe you want to use k in that if check. Something like

if (currentMin > m[j][k]){
    currentMin = m[j][k];
    currentMinIndex = k;
}

As cited per ergonaut, you have a problem with the codeblock

if(currentMin > m[j][i]) ...

and also

m[currentMinIndex][j] = m[j][i];

However, you also have a problem with your for-loops.

for(int j = 0; j < m[j].length - 1; j++) ...
    for(int i = 0; i < m.length; i++) ...

Both of these are oddly structured. You probably want to swap these for-loops so that you don't throw index exceptions. This will also cause you address the indices in your code. And modify your j-index for-loop to include the entire range.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM