title: How to sort the boundary elements of a matrix in ascending order?
body: <p>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?</p>
  
  <pre><code>    int k,temp=0,sum=0;
  
             k=n;
          boolean b=true;
          do
          {
          for(i=0;i&lt;m;i++)
          {
              for(j=0;j&lt;k-1;j++)
              {
                  if(i!=0||j!=0)
                  {
                      if(A[i][j]&gt;A[i][j+1])
                      {
                          temp=A[i][j];
                          A[i][j]=A[i][j+1];
                          A[i][j+1]=temp;
                      }
                  }
              }
          }
          k-=1;
          if(k&lt;0)
          b=false;
      }while(b);
      k=m;
      do
      {
      for(i=0;i&lt;k-1;i++)
      {
        for(j=0;j&lt;n;j++)
        {
            if(i!=0||j!=0)
            {
                if(A[j][i]&gt;A[j][i+1])
                {
                    temp=A[j][i];
                    A[j][i]=A[j][i+1];
                    A[j][i+1]=temp;
                  }
              }
          }
      }
      k-=1;
      if(k&lt;0)
      b=false;
  }while(b);
  System.out.println("REARRANGED MATRIX:");
  for(i=0;i&lt;m;i++)
         {
             for(j=0;j&lt;n;j++)
             {
                 System.out.print(A[i][j]+" ");
              }
              System.out.println();
          }
  </code></pre>