sort + search multidimensional array

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: sort + search multidimensional array

1. rix
Registered User
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. Senior Member
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?

3. rix
Registered User
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. Senior Member
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.

5. rix
Registered User
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. Senior Member
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?

7. rix
Registered User
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:");
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)
{
}
else

}
}
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. Senior Member
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]);

}
}
}

9. rix
Registered User
Join Date
Nov 2003
Posts
29
thanks again

10. Senior Member
Join Date
Mar 2003
Posts
834
No probs.

#### Posting Permissions

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

 FAQ Latest Articles Java .NET XML Database Enterprise