如何按升序对矩阵的边界元素进行排序?

时间:2015-09-23 18:33:51

标签: matrix

我已经研究过这个但是当我进入矩阵时,矩阵中的所有元素都被排序了!但我想按升序排序边界元素。有人可以告诉我我的错误吗?

    int k,temp=0,sum=0;

           k=n;
        boolean b=true;
        do
        {
        for(i=0;i<m;i++)
        {
            for(j=0;j<k-1;j++)
            {
                if(i!=0||j!=0)
                {
                    if(A[i][j]>A[i][j+1])
                    {
                        temp=A[i][j];
                        A[i][j]=A[i][j+1];
                        A[i][j+1]=temp;
                    }
                }
            }
        }
        k-=1;
        if(k<0)
        b=false;
    }while(b);
    k=m;
    do
    {
    for(i=0;i<k-1;i++)
    {
      for(j=0;j<n;j++)
      {
          if(i!=0||j!=0)
          {
              if(A[j][i]>A[j][i+1])
              {
                  temp=A[j][i];
                  A[j][i]=A[j][i+1];
                  A[j][i+1]=temp;
                }
            }
        }
    }
    k-=1;
    if(k<0)
    b=false;
}while(b);
System.out.println("REARRANGED MATRIX:");
for(i=0;i<m;i++)
       {
           for(j=0;j<n;j++)
           {
               System.out.print(A[i][j]+" ");
            }
            System.out.println();
        }

2 个答案:

答案 0 :(得分:0)

不使用条件'if(i!= 0 || j!= 0)'使用'if(i == 0 || i == 2 || j == 0 || j == 2) '。这可以解决你所遇到的歧义。你的错误是你已经将行数和列数都大于零。边界元素是行数为0或2或列数为0的那些。或2(我的意思是只有那些坐标为i = 0或i = 2或j = 0或j = 2的元素才会被视为边界元素。matrix coordinates

答案 1 :(得分:0)

我有一个解决方案。我已经使用选择排序来执行此操作。首先,我对矩阵进行了排序,然后显示了排序数组的边界

import java.io.*;

class Boundary_Sorting
{
    public static void main(String args[])throws IOException
    {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);

        System.out.println("Enter the rows of the matrix=");
        int m = Integer.parseInt(br.readLine());

        System.out.println("Enter the column of the matrix=");
        int n = Integer.parseInt(br.readLine());

        int a[][] = new int[m][n];

        int i,j;
        System.out.println("Enter the elements of the matrix: ");

        for(i = 0; i < m; i++)
        {
            for(j = 0; j < n; j++)
            {
                 a[i][j]=Integer.parseInt(br.readLine());
            }
        }

        System.out.println("**********************");
        System.out.println("The original matrix is");
        System.out.println("**********************");

        for(i = 0; i < m; i++)
        {
            for(j = 0; j < n; j++)
            {
                System.out.print(a[i][j]+"\t");
            }

            System.out.println();
        }

        int B[] = new int[m*n]; //creating a 1D Array of size 'r*c'
        int x = 0;

        for(i = 0; i < m; i++)
        {
            for(j = 0; j < n; j++)
            {
                B[x] = a[i][j];
                x++;
            }
        }

        /*Sorting the 1D Array in Ascending Order*/
        int t = 0;

        for(i = 0; i < (m * n) - 1; i++)
        {
            for(j = i + 1; j < (m * n); j++)
            {
                if(B[i] > B[j])
                {
                    t = B[i];
                    B[i] = B[j];
                    B[j] = t;
                }
            }
        }

        /*Saving the sorted 1D Array back into the 2D Array */
        x = 0;

        for(i = 0; i < m; i++)
        {
            for(j = 0; j < n; j++)
            {
                a[i][j] = B[x];
                x++;
            }
        }

        /* Printing the sorted 2D Array */
        System.out.println("**********************");
        System.out.println("The Sorted Array:");
        System.out.println("**********************");

        for(i = 0; i < m; i++)
        {
            for(j = 0; j < n; j++)
            {
                System.out.print(a[i][j]+"\t");
            }

            System.out.println();
        }

        System.out.println("**********************");
        System.out.println("The boundary elements of the matrix is=");
        System.out.println("**********************");

        for(i = 0; i < m; i++)
        {
            for(j = 0; j < n; j++)
            {
                if(i==0 || j==0 || i == m-1 || j == n-1) //condition for accessing boundary elements
                    System.out.print(a[i][j]+"\t"); 
                else
                    System.out.print(" \t"); 
            }

            System.out.println();
        }
    }
}