GeeksforGeeks » Miscellaneous

Print the matrix in a sorted order

(4 posts)

Tags:

  1. Minu
    guest
    Posted 2 years ago #

    Given a m*n matrix that is sorted row-wise and column-wise. How would you print the entire matrix in a sorted order ?

  2. Kapil
    guest
    Posted 2 years ago #

  3. Dheeraj
    Member
    Posted 2 years ago #

    One straightforward way is to divide the matrix in two and recursively merge rows (or columns). Print the final merged result.

       1     3     4
       2     4     6
       5     8     9
       7   10   11
    

    For the above example:
    1) Merge first two rows and last two rows.
    2) Finally, merge the merged arrays (obtained in step 1), and print the resultant array.

  4. gvk
    Member
    Posted 4 months ago #

    #include <stdio.h>
    #define ROWS 3
    #define COLS 5
    
    void swap(int *a, int *b)
    {
    /*swap numbers a & b*/
    	int temp = *a;
    	*a = *b;
    	*b = temp;
    }
    void print_array(int *arr)
    {
    /*prints the two dimensional array arr*/
    	int i,j;
    	for(i=0;i<ROWS;i++)
    	{
    		for(j=0;j<COLS;j++)
    		{
    			printf(" %d ",arr[COLS*i + j]);
    		}
    		printf("\n");
    	}
    }
    int min(int a, int b)
    {
    /*Finds the minimum of a and b*/
    	if(a<b)
    		return a;
    	return b;
    }
    void adjustrow(int *arr,int cur_row,int cur_col)
    {
    /*adjusts the row in ascending order*/
    	if(arr[COLS*cur_row + cur_col+1] >= arr[COLS*cur_row + cur_col]) //elem in next column is greater
    			return;
    	swap(&arr[COLS*cur_row + cur_col+1],&arr[COLS*cur_row + cur_col]);
    	adjustrow(arr,cur_row,cur_col+1);
    }
    void adjustcol(int *arr,int cur_row,int cur_col)
    {
    /*adjusts the column in asceding order*/
    	if(arr[COLS*(cur_row+1) + cur_col] >= arr[COLS*cur_row + cur_col]) //elem in next row is greater
    			return;
    	swap(&arr[COLS*(cur_row+1) + cur_col],&arr[COLS*cur_row + cur_col]);
    	adjustcol(arr,cur_row+1,cur_col);
    }
    void readjust(int *arr,int cur_row, int cur_col)
    {
    /*re-adjusts the array so that the initial property of ascending rows and columns is maintained*/
    	if(cur_row == ROWS-1) //into the last row
    	{
    		adjustrow(arr,cur_row,cur_col);
    		return;
    	}
    	if(cur_col == COLS -1) //into the last column
    	{
    		adjustcol(arr,cur_row,cur_col);
    		return;
    	}
    	//new elem in right place
    	if(arr[COLS*cur_row + cur_col] <= min(arr[COLS*(cur_row+1) + cur_col],arr[COLS*(cur_row) + cur_col+1]))
    		return;
    	int new_cur_row = arr[COLS*(cur_row+1) + cur_col] <= arr[COLS*cur_row + cur_col+1] ? (cur_row+1) : cur_row;
    	int new_cur_col = arr[COLS*(cur_row+1) + cur_col] <= arr[COLS*cur_row + cur_col+1] ? cur_col : (cur_col+1);
    	swap(&arr[COLS*cur_row + cur_col],&arr[COLS*new_cur_row + new_cur_col]);
    	readjust(&arr[0],new_cur_row,new_cur_col);
    }
    void sort_whole_array(int *arr)
    {
    /*interface function for sorting the complete array*/
    	int i,j;
    	for(i=0;i<ROWS-1;i++)
    	{
    		for(j=1;j<COLS;j++)
    		{
    			if(arr[COLS*i + j] <= arr[COLS*(i+1) + 0]) //arr[i][j] <= arr[i+1][0]
    				continue;
    			swap(&arr[COLS*i + j],&arr[COLS*(i+1) + 0]); //swap(&arr[i][j],&arr[i+1][0])
    			readjust(&arr[0],(i+1),0); //readjust(&arr[i+1][0],i+1,0);
    		}
    	}
    }
    int main()
    {
    	int arr[ROWS][COLS] = {{1,3,4,8,9},{2,5,18,25,50},{6,7,22,45,55}};
    	printf("\n==Input Array==\n");
    	print_array(&arr[0][0]);
    	sort_whole_array(&arr[0][0]);
    	printf("\n==Sorted Array==\n");
    	print_array(&arr[0][0]);
    	return 0;
    }
    

Reply

You must log in to post.

RSS feed for this topic