DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2003
    Posts
    29

    sort + search multidimensional array

    hey all

    I cant seem to find any syntax to allow searching or sorting in multidimensional arrays. I have a [4][5] array that i need to sort and then binary search to return an index position for any one element. On the sun website the binary search reference shows a single int[] as an argument but there is no mention of multidimensions.

    thanks

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    That's because it doesn't really make sense in multi-dimension space! What are you actually trying to solve?

    Consider:
    Code:
    {
      {2, 5, 8, 9}
      {5, 8, 2, 2, 2}
      {9, 2, 5}
    }
    ...after sorting each dimension:
    Code:
    {
      {2, 5, 8, 9}
      {2, 2, 2, 5, 8}
      {2, 5, 9}
    }
    Now what? Let's say you wanted to find 5. It could appear in several indices:
    Code:
    {
      {2, 5, 8, 9}
      {2, 2, 2, 5, 8}
      {2, 5, 9}
    }
    ...so you could do a binary search on each index and store each of the two indices ([0][1], [1][3] and [2][1]). What about duplicate data items?
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Nov 2003
    Posts
    29
    I jjust want to sort the array, index by index, i presume with a similar manner i can search the array. Do you know the syntax required to sort the array ?

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    You're missing the point - WHICH array? As you can see from my example above you have an array which contains three arrays.
    ArchAngel.
    O:-)

  5. #5
    Join Date
    Nov 2003
    Posts
    29
    ha ha, sorry im not being very specific. I was wanting to search the first row, the second row etc im trying to make loops to do this but i cant figure out the syntax.

  6. #6
    Join Date
    Mar 2003
    Posts
    834
    You're still not being very specific!

    Is it that you want to find the smallest value in an array of arrays?
    ArchAngel.
    O:-)

  7. #7
    Join Date
    Nov 2003
    Posts
    29
    Code:
    import java.util.*;
    class TwoDArray {
    public static void main (String args[]) {
    
    int foundAtIndex = 0;
     boolean test = false;
     int twoD[][]=new int [4][5];
     int i, j, k =0;
     for (i=0;i<4;i++) {
    	for (j=0;j<5;j++) {
    	k = (int)(Math.random()*5+1);
    	twoD[i][j]=k;
    			}
    			}  
    
    //Arrays.Sort(twoD);   
    
    for (i=0;i<4;i++) {
    for (j=0;j<5;j++) {
    	System.out.print (twoD[i][j]);
    		 }
    	System.out.println();
    		}
    
    KeyboardInput in = new KeyboardInput(); 
    
    System.out.println("\nPlease enter integer you wish to find:");
    int intToFind=in.readInteger();
    for (i=0;i<4;i++) {
    for (j=0;j<5;j++) {
    //int x = Arrays.binarySearch(twoD,intToFind);
    if (intToFind==twoD[i][j])
    	{
    test=true;
    	}
    }
    }
    
    if (test == true)
    {
    System.out.print("Your integer was found ");
    }
    else
    System.out.print("Integer not found");
    
    }
    }
    Obviously for now it will only return the first instance of element it searches for, i'll deal with multiple instances later. Ideally i'll like to know how:

    -to sort each row
    -to search each row with binarySearch so i can be given the index value the element was found at.

    hope that helps, thanks

  8. #8
    Join Date
    Mar 2003
    Posts
    834
    It's all in the java.util.Arrays class. This code will sort your multi-dimension array. From this you should be able to work out the binary search code.
    Code:
    import java.util.Arrays;
    
    public class Test {
    	public static void main(String[] args) {
    		int[][] myArray = { {2, 5, 8, 9}, {5, 8, 2, 2, 2}, {9, 2, 5} };
    		display(myArray);
    		sortAll(myArray);
    		display(myArray);
    	}
    		
    	public static void display(int[][] myArray) {
    		// Move through the first dimension...
    		for (int i = 0; i < myArray.length; i++) {
    
    			// Move through the second dimension...
    			for (int j = 0; j < myArray[i].length; j++) {
    
    				// Display the element.
    				System.out.print(myArray[i][j] + "\t");
    
    			}
    
    			System.out.println();
    		}
    		System.out.println();
    	}
    
    		
    	public static void sortAll(int[][] myArray) {
    		// Move through the first dimension...
    		for (int i = 0; i < myArray.length; i++) {
    
    			// Sort the next dimension.
    			Arrays.sort(myArray[i]);	
    
    		}
    	}
    }
    ArchAngel.
    O:-)

  9. #9
    Join Date
    Nov 2003
    Posts
    29
    thanks again

  10. #10
    Join Date
    Mar 2003
    Posts
    834
    No probs.
    ArchAngel.
    O:-)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links