sorting a 2d array - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# sorting a 2d array

Posts: 47Member
Hi

I want to sort a 2d integer array with bubble,selection and quick sort algorithms.
I have a problem to compare the last element of the first row with the first element of second row
i.e f(or a NxN array with N=5)
[0][4] with [1][0]
[1][4] with [2][0]
.
.
.

My idea is to copy the elements of the 2d array to 1d array,sort the 1d array and finally copy back the elements to the 2d array.
I dont think that's the best way...Can you give me some ideas on how to sort a 2d array with these methods(bubble ,selection and quick sort )

• Posts: 9,765Member ✭✭✭
[b][red]This message was edited by stober at 2004-10-29 10:2:55[/red][/b][hr]
: Hi
:
: I want to ask something...
: I want to sort a 2d integer array with bubble,selection and quick sort algorithms.
: I have a problem to compare the last element of the first row with the first element of second row
: i.e f(or a NxN array with N=5)
: [0][4] with [1][0]
: [1][4] with [2][0]
: .
: .
: .
:
: My idea is to copy the elements of the 2d array to 1d array,sort the 1d array and finally copy back the elements to the 2d array.
: I dont think that's the best way...Can you give me some ideas on how to sort a 2d array with these methods(bubble ,selection and quick sort )
:
:

[blue]Are you certain you need to sort all the elements in that 2d array in ascending (or descending) order from top to bottom and left to right? It doesn't make any sense to do that. The columns in a 2d array usually have some relationship with each other, and scrambling them up completly destroys that relationship. For example, let say that the array holds the last 5 test scores for all the students in your class. So the vertical cells (rows) represent the student,and the horizontal cells (columns) are the test scores. Mixing sudent #1's scores with student #2's scores makes no sense (and may make student #2 very piss** off at you!:-)) So usually matrixes are sorted by either one column (arrange the rows by student ID for example) or by row (put each student's test scores in numerical order.[/blue]

• Posts: 47Member
: [b][red]This message was edited by stober at 2004-10-29 10:2:55[/red][/b][hr]
: : Hi
: :
: : I want to ask something...
: : I want to sort a 2d integer array with bubble,selection and quick sort algorithms.
: : I have a problem to compare the last element of the first row with the first element of second row
: : i.e f(or a NxN array with N=5)
: : [0][4] with [1][0]
: : [1][4] with [2][0]
: : .
: : .
: : .
: :
: : My idea is to copy the elements of the 2d array to 1d array,sort the 1d array and finally copy back the elements to the 2d array.
: : I dont think that's the best way...Can you give me some ideas on how to sort a 2d array with these methods(bubble ,selection and quick sort )
: :
: :
:
: [blue]Are you certain you need to sort all the elements in that 2d array in ascending (or descending) order from top to bottom and left to right? It doesn't make any sense to do that. The columns in a 2d array usually have some relationship with each other, and scrambling them up completly destroys that relationship. For example, let say that the array holds the last 5 test scores for all the students in your class. So the vertical cells (rows) represent the student,and the horizontal cells (columns) are the test scores. Mixing sudent #1's scores with student #2's scores makes no sense (and may make student #2 very piss** off at you!:-)) So usually matrixes are sorted by either one column (arrange the rows by student ID for example) or by row (put each student's test scores in numerical order.[/blue]
:
:
:
:
: YES
This is an exercise
It says to sort the 2d array with these methods
and it says that the next element of the [i][N-1] is the [i+1][0]
What i must do ??
I am desperated
• Posts: 9,765Member ✭✭✭
[b][red]This message was edited by stober at 2004-10-29 13:21:0[/red][/b][hr]
an array of ints is just one big blob or memory, so you can treat the 2d array as a 1d array without resorting to creating a 1d array and copying the data. All you need to do is redefine (using typcasting)the array. In the example below, all I did was redefined the array as a pointer p, then used p as if it were a 1d array.

There are better and faster bubble sort algorithms than the one I show below.

[code]
#include
#include
#include

const int ROWS = 5;
const int COLS = 5;

int array[ROWS][COLS];

void bubbleSort()
{
int* p = (int*)array;
int i,j;
for(i = 0; i < ((ROWS * COLS)-1);i++)
{
for(j = i+1; j < (ROWS * COLS); j++)
{
if(p[i] > p[j])
{
int hold = p[i];
p[i] = p[j];
p[j] = hold;
}
}
}
}

void ShowArray()
{
int i,j;
for(i = 0; i < ROWS;i++)
{
for(j =0; j < COLS; j++)
{
printf("%5d ", array[i][j]);
}
printf("
");
}

}

int main(int argc, char* argv[])
{
int i,j,n;
srand(time(0));
// Fill the array with random numbers
for(i = 0; i < ROWS;i++)
{
for(j =0; j < COLS; j++)
{
while( (n = rand()) == 0)
;
array[i][j] = n;
}
}

ShowArray();
printf("
");
//sort the array
bubbleSort();
//display the sorted array
ShowArray();
getchar();
return 0;
}
[/code]

• Posts: 47Member
: [b][red]This message was edited by stober at 2004-10-29 13:21:0[/red][/b][hr]
: an array of ints is just one big blob or memory, so you can treat the 2d array as a 1d array without resorting to creating a 1d array and copying the data. All you need to do is redefine (using typcasting)the array. In the example below, all I did was redefined the array as a pointer p, then used p as if it were a 1d array.
:
: There are better and faster bubble sort algorithms than the one I show below.
:
: [code]
: #include
: #include
: #include
:
: const int ROWS = 5;
: const int COLS = 5;
:
: int array[ROWS][COLS];
:
: void bubbleSort()
: {
: int* p = (int*)array;
: int i,j;
: for(i = 0; i < ((ROWS * COLS)-1);i++)
: {
: for(j = i+1; j < (ROWS * COLS); j++)
: {
: if(p[i] > p[j])
: {
: int hold = p[i];
: p[i] = p[j];
: p[j] = hold;
: }
: }
: }
: }
:
: void ShowArray()
: {
: int i,j;
: for(i = 0; i < ROWS;i++)
: {
: for(j =0; j < COLS; j++)
: {
: printf("%5d ", array[i][j]);
: }
: printf("
");
: }
:
:
: }
:
: int main(int argc, char* argv[])
: {
: int i,j,n;
: srand(time(0));
: // Fill the array with random numbers
: for(i = 0; i < ROWS;i++)
: {
: for(j =0; j < COLS; j++)
: {
: while( (n = rand()) == 0)
: ;
: array[i][j] = n;
: }
: }
:
: ShowArray();
: printf("
");
: //sort the array
: bubbleSort();
: //display the sorted array
: ShowArray();
: getchar();
: return 0;
: }
: [/code]
:
:
:
:
:
:
:
:
:
thanks a lot my friend....
now with this method i ll try to sort with selection and quicksort ...
wish me luck
thanks again!
theodor