# how to sort a 2D array

This is a discussion on how to sort a 2D array within the C Programming forums, part of the General Programming Boards category; hi there, i am trying to sort a 2d array with no luck... this is what i have for a ...

1. ## how to sort a 2D array

hi there,
i am trying to sort a 2d array with no luck... this is what i have
for a single array.
any help would be appreciated.

int array[7] = {'6','3','1','4','2','5','7'};
for(i=0;i<7;i++)

for(j=0;j<7;j++)
{
if(array[j+1] < array[j])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;

}

}

2. int arrays are not stored like what you did, you in fact put it as a character array because of the 's

3. granted.....but any pointers for me on the 2d array side of things.

4. Post some code that you have used to try and sort the 2d array, and we'll have a look at whats going on.

Also, what type of array is it ? chars, ints or what?

5. try this
Code:
```int array[7] = {6,3,1,4,2,5,7};
for(i=0;i<7;i++)
for(j=0;j<7;j++)
{
if(array[j+1] < array[j])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;

}

}```
I think thats right...

6. ## bubble sort for 2d array problems?

i am using an int array. this is how i am trying to solve the problem, but to no avail.....

int array[3][2] = {{3,6},{5,2},{1,4}};

for(i=0;i<3;i++)

for(i=0;i<3;i++)
for(j=0;j<2;j++)
{
if(array[i+1][j+1] < array[i][j])
{
temp = array[i][j];
array[i][j] = array[i+1][j+1];
array[i+1][j+1] = temp;
}
}

7. > {{3,6},{5,2},{1,4}};
Are you trying to get to

{{1,4},{3,6},{5,2}};
ie, the first number is the sort key, and you want to keep the associated second number with it?

8. no, all i want to do is sort the array, starting from the lowest up
to the top.

int array[3][2] = {{3,6},{5,2},{1,4}};

so i want to finish with the 2d array like the following,

int array[3][2] = {{1,2},{3,4},{5,6}};

Sean.

9. Originally posted by Lynux-Penguin
try this
Code:
```int array[7] = {6,3,1,4,2,5,7};
for(i=0;i<7;i++)
for(j=0;j<7;j++)
{
if(array[j+1] < array[j])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;

}

}```
I think thats right...
I think you've exceeded the array bounds when you did array[j+1] when j was 6.

10. 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;
}```