Results 1 to 6 of 6
  1. #1
    anfielder is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default 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));
    	}
    }
    For the sorting part, I just put in random numbers for it to work. My issue, is incorporating the arrays [i][j] and sorting it both vertically and horizontally.
    I'll appreciate all the input

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  3. #3
    anfielder is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    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;
    
    	}
    }

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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 sort-rows/transpose/sort-rows/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; 12-10-2010 at 09:17 PM.

  5. #5
    anfielder is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    Transposing is still not working. Sorting works only horizontally and not vertically. Tried your suggestion but it still doesn't work

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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

  1. two dimensional arrays
    By cliffh in forum New To Java
    Replies: 2
    Last Post: 11-05-2010, 11:43 PM
  2. Help with java sorting two dimensional array
    By Joycey in forum New To Java
    Replies: 2
    Last Post: 03-27-2010, 02:36 AM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  4. dynamic two dimensional arrays?
    By dinosoep in forum New To Java
    Replies: 4
    Last Post: 12-05-2009, 06:12 PM
  5. Multi-dimensional arrays
    By Implode in forum New To Java
    Replies: 1
    Last Post: 09-15-2009, 08:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •