sorting a 2d array

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 )

Comments

[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]

: [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]: : : : : YESThis is an exerciseIt says to sort the 2d array with these methodsand it says that the next element of the [i][N-1] is the [i+1][0]What i must do ??I am desperated

[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.