From the code snippet you provided I am assuming you were trying to sort each row of the matrix separately. The first thing I noticed is that there is a typo in the memory allocation of columns (2nd index) of the matrix.
Correct memory allocation of a numRow x numColumns matrix would be as follows:
/* loop counter */
int i;
/* dynamic array sizes */
const int numRow = 5;
const int numColumns = 25;
/* allocate the row pointers */
int **dynamic2d = (int **)malloc(numRow * sizeof(int *));
/* for each row pointer */
for(i = 0; i < numRow; i++)
{
/* allocate columns */
dynamic2d[i] = (int *)malloc(numColumns * sizeof(int));
}
Next you won't be able to simply call the qsort(..) method only once. That method expects a "flat" or one-dimensional array. You will need to call the qsort(...) method separately for each row of the matrix. This is demonstrated below:
/* sort array */
for(i = 0; i < numRow; i++)
qsort(dynamic2d[i], numElements, sizeof(int *), comp);
Lastly, you made a mistake with your comparator method. This method has strict rules that need to be followed in order to work correctly. Current specifications say, "The application shall ensure that the function returns an integer less than, equal to, or greater than 0, if the first argument is considered respectively less than, equal to, or greater than the second. If two members compare as equal, their order in the sorted array is unspecified."
This is a simple fix. Simply write the logic to produce those results as seen below:
int comp(const void* firstArg, const void* secondArg)
{
/* get the values of the arguments */
int first = *(int *)firstArg;
int second = *(int *)secondArg;
/* return the value as expected by the qsort() method */
if(first < second)
{
return 1;
}
else if(second < first)
{
return -1;
}
return 0;
}
Last thing to note, this will sort greatest to least. Do not switch the logic around in the comparator if you want least to greatest. The sort will not return accurate results. The correct way to do this is by reading the array from back to front as seen below: You can swap the arguments in the comparator to change the sorting order or read the results from back to front.
int comp(const void* firstArg, const void* secondArg)
{
/* get the values of the arguments */
int first = *(int *)secondArg;
int second = *(int *)firstArg;
...
}
or
/* print greatest to smallest */
for(i = 0; i < numRow; i++)
{
/* start at front and work to back */
for(j = 0; j < numColumns; j++)
printf("%d ", dynamic2d[i][j]);
printf("\n");
}
/* print smallest to greatest */
for(i = 0; i < numRow; i++)
{
/* start at back and work to front */
for(j = numColumns- 1; j >= 0; j--)
printf("%d ", dynamic2d[i][j]);
printf("\n");
}
Hopefully this helps! If you need to sort the entire matrix as a whole... that is a different beast all together.
compfunction is wrong. What ifais the minimum possible integer value andbis 1? Thena - bwill be the maximum possible integer value (on most systems) because of wraparound in integer operations, which is positive even though the result ofcompshould be negative.