Results 1 to 6 of 6
Thread: Sorting Two Dimensional arrays
 12092010, 09:37 PM #1Member
 Join Date
 Dec 2010
 Posts
 6
 Rep Power
 0
Sorting Two Dimensional arrays
I have encountered a problem in sorting two D arrays. Basically, I'm trying to have my arrays sorted out horizontally first and then vertically. After researching, I found a way to sort a list of numbers but having arrays seems to be a pain. Here's my code so far:
Java Code:import java.util.Arrays; import java.util.Scanner; public class TwoDArray { public static void main(String [] args) { Scanner input = new Scanner(System.in); System.out.println("enter the size i: "); int k=input.nextInt(); System.out.println("enter the size of j: "); int l=input.nextInt(); int arr[][] = new int[l][k]; int i=0, j=0; while (i<k) { while(j<l) { System.out.println("arr["+i+"]["+j+"]: "); arr[i][j]=input.nextInt(); j+=1; }j=0;//end of while j i+=1; }i=0;//end of while i while(i<k) { while (j<l) { System.out.print(arr[i][j]+"\t"); j+=1; }j=0; i+=1; System.out.println(); }i=0; double [] lengths = {23,12,32,83,45}; Arrays.sort(lengths); System.out.println(Arrays.toString(lengths)); } }
I'll appreciate all the input
 12092010, 10:18 PM #2Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 14
I'm trying to have my arrays sorted out horizontally first and then vertically.
What is the vertical sort supposed to do: sort on the value of the first element of the row? the length of the row? something else? If the first of these how will empty rows be handled?

In general Arrays.sort() is good. It can be used to sort all the rows into ascending order. And then can be applied to sort the rows: you have to supply a comparator for that. And that depends on having a specific vertical sort in mind.
 12102010, 09:17 PM #3Member
 Join Date
 Dec 2010
 Posts
 6
 Rep Power
 0
I managed sorting the numbers horizontally using the general formula you provided above. The only problem is with sorting the numbers vertically. I was given the idea of transforming the axis but I've had a few problems.
Check out my code so far
Java Code:import java.util.Arrays; import java.util.Scanner; public class TwoDArray { public static void main(String [] args) { Scanner input = new Scanner(System.in); System.out.println("enter the size i: "); int k=input.nextInt(); System.out.println("enter the size of j: "); int l=input.nextInt(); int arr[][] = new int[l][k]; int arr2[][] = new int [k][l]; int i=0, j=0; while (i<l) { while(j<k) { System.out.println("arr["+i+"]["+j+"]: "); arr[i][j]=input.nextInt(); j+=1; }j=0;//end of while j i+=1; }i=0;//end of while i while (i<l) { Arrays.sort(arr [i]); i++; }i=0; while(i<l) { while (j<k) { System.out.print(arr[i][j]+"\t"); j+=1; }j=0; i+=1; System.out.println(); }i=0; arr2[j][i] = arr[i][j]; while (j<k) { Arrays.sort(arr [i]); i++; }i=0; } }
 12102010, 10:14 PM #4Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 14
I'm still unsure what the vertical sort is supposed to do. I had imagined that it was sorting whole lines up and down. But is your intention to sort elements of a column up and down?
If so your idea of "transforming the axis"  ie transposing the matrix  is the way to go. (The matrix must be rectangular, of course).
The general plan of attack is:
* Sort each row
* Transpose the matrix
* Sort each row again (they are now really the columns)
* Transpose the matrix back again
Java Code:arr2[j][i] = arr[i][j];
This does very little. Perhaps you mean:
Java Code:for(int row = 0; row < l; row++) { for(int col = 0; col < k; col++) { arr2[col][row] = arr[row][col]; } }

I also prefer for loops for the sorting:
Java Code:for(int row = 0; row < k; row++) { Arrays.sort(arr[row]); }
To keep your thinking straight choose meaningful variable names. I would go for arr and arrT. And perhaps row and col for variables (defined locally within the for loops). And numRows and numCols if you need them in place of k and the nasssty l. Row and column, of course, are relative to arr rather than the transposed arrT.
Just my ideas for names... But whatever you do keep rows and columns straight in your mind.

Ask if the sortrows/transpose/sortrows/transpose doesn't make sense (or if you think it's wrong). It seemed to be sort of what you were doing.Last edited by pbrockway2; 12102010 at 10:17 PM.
 12142010, 04:36 AM #5Member
 Join Date
 Dec 2010
 Posts
 6
 Rep Power
 0
Transposing is still not working. Sorting works only horizontally and not vertically. Tried your suggestion but it still doesn't work
 12142010, 08:12 AM #6Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 14
Transposing is still not working.
There's not a lot to go on here.
Perhaps you could post some (runnable) code and say what its output/behaviour is?
I'm *still* not sure what you mean by sorting vertically. Is it this:
Java Code:1 4 6 7 1 4 6 7 1 2 3 5 5 3 2 2 > 2 2 3 5 > 2 4 6 7 2 6 8 7 2 6 7 8 2 6 7 8
Similar Threads

two dimensional arrays
By cliffh in forum New To JavaReplies: 2Last Post: 11062010, 12:43 AM 
Help with java sorting two dimensional array
By Joycey in forum New To JavaReplies: 2Last Post: 03272010, 03:36 AM 
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03232010, 02:40 AM 
dynamic two dimensional arrays?
By dinosoep in forum New To JavaReplies: 4Last Post: 12052009, 07:12 PM 
Multidimensional arrays
By Implode in forum New To JavaReplies: 1Last Post: 09152009, 08:50 AM
Bookmarks