This should do
Code:
#include <stdio.h>
#define N_COLS 2
#define N_ROWS 3
/* Sort a 2D array */
/* Treat the array as a linear array, but in doing so, we */
/* need to reconstuct the effective row and column indices */
/* back into the 2D array */
void sort ( int arr[][N_COLS], int size ) {
int lim = size * N_COLS;
int i, j;
for ( i = lim-1 ; i >= 0 ; i-- ) {
int ri = i / N_COLS;
int ci = i % N_COLS;
for ( j = 0 ; j < i; j++ ) {
int rj = j / N_COLS;
int cj = j % N_COLS;
if ( arr[ri][ci] < arr[rj][cj] ) {
int temp = arr[ri][ci];
arr[ri][ci] = arr[rj][cj];
arr[rj][cj] = temp;
}
}
}
}
void print ( int arr[][N_COLS], int size ) {
int r, c;
for ( r = 0 ; r < size ; r++ ) {
for ( c = 0 ; c < N_COLS ; c++ ) {
printf( "%d ", arr[r][c] );
}
printf( "\n" );
}
}
int main ( ) {
int array[N_ROWS][N_COLS] = {{3,6},{5,2},{1,4}};
printf( "Unsorted\n" );
print( array, N_ROWS );
sort( array, N_ROWS );
printf( "\nsorted\n" );
print( array, N_ROWS );
return 0;
}