>sort a Randomized Dynamic 2D Matrix of Integers
That could be a problem, but first I'll describe how you would do it with a real array.
>how Qsort will help me?
You cheat and give qsort something that it can work with.
>if a 2D array is one big 1D arrray
Yes, and no. In memory, a two-dimensional array is stored contiguously, so yes it is a big one-dimensional array. However, the C (and C++) standard doesn't allow you to portably treat a two-dimensional array as a one-dimensional array. However, you can try it, and it'll probably work:
#include <stdio.h>
#include <stdlib.h>
#define ROWS 4
#define COLS 4
int cmp ( const void *a, const void *b )
{
const int *pa = a, *pb = b;
if ( *pa < *pb )
return -1;
else if ( *pa > *pb )
return +1;
else
return 0;
}
int main ( void )
{
int a[ROWS][COLS];
int i, j;
for ( i = 0; i < ROWS; i++ ) {
for ( j = 0; j < COLS; j++ ) {
int r = rand() % 100;
a[i][j] = r;
printf ( "%d ", r );
}
puts ( "" );
}
puts ( "" );
/* Not portable */
qsort ( a, ROWS * COLS, sizeof ( int ), cmp );
for ( i = 0; i < ROWS; i++ ) {
if ( i % 2 == 0 ) {
for ( j = 0; j < COLS; j++ )
printf ( "%d ", a[i][j] );
}
else {
for ( j = COLS - 1; j >= 0; j-- )
printf ( "%d ", a[i][j] );
}
puts ( "" );
}
return 0;
}
You can do a similar feat of educated type mangling in C++ as well:
#include <iostream>
#include <algorithm>
using namespace std;
const int ROWS = 4;
const int COLS = 4;
int main()
{
int a[ROWS][COLS];
for ( int i = 0; i < ROWS; i++ ) {
for ( int j = 0; j < COLS; j++ ) {
int r = rand() % 100;
a[i][j] = r;
cout<< r <<' ';
}
cout<<'\n';
}
cout<<'\n';
/* Not Portable */ {
int *pa = (int *)a;
sort ( pa, pa + ( ROWS * COLS ) );
}
for ( int i = 0; i < ROWS; i++ ) {
if ( i % 2 == 0 ) {
for ( int j = 0; j < COLS; j++ )
cout<< a[i][j] <<' ';
}
else {
for ( int j = COLS - 1; j >= 0; j-- )
cout<< a[i][j] <<' ';
}
cout<<'\n';
}
}
Officially, I don't recommend such tickery, but unofficially, it's probably your most efficient option. Sorting algorithms typically expect only one dimension to work with, so standard solutions don't quite cut it. Your other option would be to devise a custom sorting algorithm that handles a two-dimensional array (not hard, but still takes time), or do an uncomfortable amount of copying. Both of which are likely to be less efficient for both execution and development time.
If you can't just print in the order that's most convenient, then the fastest solution would probably be to reverse every other row:
#include <stdio.h>
#include <stdlib.h>
#define ROWS 4
#define COLS 4
int cmp ( const void *a, const void *b )
{
const int *pa = a, *pb = b;
if ( *pa < *pb )
return -1;
else if ( *pa > *pb )
return +1;
else
return 0;
}
void reverse ( int a[] )
{
int i, j;
for ( i = 0, j = COLS - 1; i < j; i++, j-- ) {
int save = a[i];
a[i] = a[j];
a[j] = save;
}
}
int main ( void )
{
int a[ROWS][COLS];
int i, j;
for ( i = 0; i < ROWS; i++ ) {
for ( j = 0; j < COLS; j++ ) {
int r = rand() % 100;
a[i][j] = r;
printf ( "%d ", r );
}
puts ( "" );
}
puts ( "" );
/* Not portable */
qsort ( a, ROWS * COLS, sizeof ( int ), cmp );
for ( i = 0; i < ROWS; i++ ) {
if ( i % 2 != 0 )
reverse ( a[i] );
for ( j = 0; j < COLS; j++ )
printf ( "%d ", a[i][j] );
puts ( "" );
}
return 0;
}
With an easier time of it in C++ because reverse is a standard algorithm:
#include <iostream>
#include <algorithm>
using namespace std;
const int ROWS = 4;
const int COLS = 4;
int main()
{
int a[ROWS][COLS];
for ( int i = 0; i < ROWS; i++ ) {
for ( int j = 0; j < COLS; j++ ) {
int r = rand() % 100;
a[i][j] = r;
cout<< r <<' ';
}
cout<<'\n';
}
cout<<'\n';
/* Not Portable */ {
int *pa = (int *)a;
sort ( pa, pa + ( ROWS * COLS ) );
}
for ( int i = 0; i < ROWS; i++ ) {
if ( i % 2 != 0 )
reverse ( a[i], a[i] + COLS );
for ( int j = 0; j < COLS; j++ )
cout<< a[i][j] <<' ';
cout<<'\n';
}
}
Now, back to the dynamic array part. Depending on how you allocate memory for the array, the above suggestion could work wonderfully (and no longer be non-portable), or nosedive miserably into the realm of pain. In other words, this works because all of the elements are contiguous in memory:
int **a = new int[ROWS * COLS];
But this doesn't because they might not be:
int **a = new int*[ROWS];
for ( int i = 0; i < ROWS; i++ )
a[i] = new int[COLS];
If you allocate memory to your dynamic array with the more common latter example, you're stuck with excessive copying or writing your own custom sorting routine that takes multiple dimensions into account.