Thread: sorting the matrix

  1. #1
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79

    sorting the matrix

    Trying to sort matrix such that it has value in ascending order.

    But outcome is coming :

    Code:
    Matrix after sorting :
    2,2,2,2,
    2,2,2,2,
    2,2,2,2,
    2,2,2,2,
    Can't find whats wrong.

    My code :

    [code]
    Trying to sort matrix such that it has value in ascending order.But outcome is coming :

    Code:
    Matrix after sorting :
    2,2,2,2,
    2,2,2,2,
    2,2,2,2,
    2,2,2,2,
    Can't find whats wrong.

    My code :

    Code:
     #include<stdio.h>
    
    
    void sort(int [][4]); 
    int main( void)
    { 
      int i,j,Matrix[4][4] ={            /*Initializing array*/				
                             2,4,3,67,
    					     6,8,2,7,
    					     9,12,6,4,
    						 12,4,5,7
    					    };	
      sort(Matrix);										
     printf ( "Matrix after sorting : \n");
      
     for(i=0;i<4;i++)
     {
       for(j=0;j<4;j++)
        {
         printf("%d,",Matrix[i][j]);
    	}
    	printf("\n");
     }
    }
    
    
    void sort(int a[4][4])
     {
     int b[4][4] ;
     int i,j,x,y,temp;
      for(i=0;i<4;i++)
     {
       for(j=0;j<4;j++)
        {
         b[i][j]=a[i][j];
    	}
     }
    	for(i=0;i<4;i++)
    	 {
    	  for(j=0;j<4;j++)
    	   {
    	     for(x=i;x<4;x++)
    		  {
    			for(y=j;y<4;y++)
    			 { 
    			  if (a[i][j] > b[x][y])
    			    temp = b[x][y];
    				b[x][y]= a[i][j];
    				a[i][j]= temp;
    			 }
    		  }
    	   }
    	 }
     }

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Why is the program making a copy of the matrix in b, it can sort a in place if it's just using swaps. The program could also "cheat" by casting a[4][4] to an array ((int *)a)[...] and use an array sort.

  3. #3
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79
    Quote Originally Posted by rcgldr View Post
    Why is the program making a copy of the matrix in b, it can sort a in place if it's just using swaps. The program could also "cheat" by casting a[4][4] to an array ((int *)a)[...] and use an array sort.
    Thank you for pointing that b matrix thing.
    Doing some modification lets see if i get it right .
    Last edited by coder1; 09-29-2013 at 04:43 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix Sorting using Pointer
    By Ponir in forum C Programming
    Replies: 7
    Last Post: 05-06-2011, 06:05 AM
  2. Sorting Matrix
    By alex 2010 in forum C++ Programming
    Replies: 0
    Last Post: 06-24-2010, 09:40 AM
  3. [C] sorting a matrix of struct
    By spaistik in forum C Programming
    Replies: 5
    Last Post: 05-24-2010, 09:18 AM
  4. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  5. Sorting a 3-d matrix
    By Glauber in forum C++ Programming
    Replies: 15
    Last Post: 05-27-2008, 04:17 AM