Eddy
Eddy

Reputation: 1

How to sort the boundary elements of a matrix in ascending order?

I've worked on this but when I'm entering the matrix, all the elements in the matrix are getting sorted! But I want to sort only the boundary elements in ascending order. Can some body please tell me my mistake?

    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();
        }

Upvotes: 0

Views: 7258

Answers (2)

Pratirup Goswami
Pratirup Goswami

Reputation: 1

I have one solution of this. I have used selection sort for doing this.At first I have sorted the matrix then displaying the boundary of the sorted array

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();
        }
    }
}

Upvotes: 0

Rahul devarakonda
Rahul devarakonda

Reputation: 1

Instead of using the condition 'if(i!=0||j!=0)' use 'if(i==0||i==2||j==0||j==2)'.This may solve the ambiguity you are having.Your mistake was that you have taken the number of rows and columns both to be greater than zero.the boundary elements are those where number of rows is 0 or 2 or number of columns is 0 or 2(by this I mean that only those elements which have coordinates with either i=0 or i=2 or j=0 or j=2 will be considered as boundary elements.matrix coordinates

Upvotes: 0

Related Questions