I have a 2-dimensional(3x7) array. I would like to sort it per row. I have written a sample code.
#define numRows 3
#define numCols 7
int arr[numRows-1][numCols-1] = {{0,1,2,3,4,5,6}, {7,8,9,10,11,12,13},{14,15,16,17,18,19,20}};
sort_row(arr);
void sort_row(int *p)
{
for (int i = 0 ;i<numRows-1;i++)
{
sort(p[i][]);
}
}
void sort(int *p)
{
for ( int c = 1 ; c <= numCols - 1 ; c++ )
{
for ( d = 0 ; d <= c - 1 ; d++ )
{
if ( array[c] < array[d] )
{
swap = array[d];
array[d] = array[c];
for ( int k = c ; k > d ; k-- )
array[k] = array[k-1];
array[k+1] = swap;
}
}
}
}
I am doing insertion sort for each row.
Qn: Am i passing the each row into sort properly? How can i improve it?
Looking for suggestion to improve that could improve or point out my mistakes in the code
d,nandswapare undeclared variables.ninsort()? Which are the values ofnumRowsandnumCols? Why isarrdimensioned tonumRows - 1andnumCols - 1?