Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Sorting multi dimensional array

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone tell me how to sort a multi dimensional array on the second dimension. I have used this two dimension array as a hit counter. What I mean is the value in the first dimension represents the number being counted, the value in the second dimension is the number of hits. Implementing a bubble sort on the second dimension resulted in a sort, but ended up altering the value in the first dimension. How can I perform this sort with out changing the values of the first dimension.

for( int outer=53; outer > 0; outer--)
{
for( int j=1; j < 54; j++ )
{
int temp[][] = new int[54][54];
if( j <= 52 )
{
if( index[j][1] > index[j + 1][1] )
{
temp[j][1] = index[j][1];
index[j][1] = index[j + 1][1];
index[j + 1][1] = temp[j][1];
}
}
}
}

 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lou -
Java doesn't support multidimensional arrays, except in appearance. They're really arrays of arrays; when you declare a [3][5] array you're not getting a 3 x 5 table, you're getting an array with three elements, each of which is an array, which all happen to have 5 elements each. The secondary arrays don't have to be the same size.
What you're doing below will indeed change the first element on the "second dimension," but that is as it should be. Use the position of the second array, not the value of its first element, as a way of tracking it -- that'll save you a lot of confusion.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Lou Caudell
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Micheal, this is what I came up with from your suggestion. It works. Is this what you meant?
Do you think this problem would be better served by another data structure.
- Lou
int index[][] = new int[54][2];
for( int i=1; i<54; i++ )
{
index[i][0] = h[i];
index[i][1] = i;
}

//for( int i=1; i<54; i++ ) System.out.println(" i=" + i + "\t" + index[i][0]);


for( int outer=53; outer > 0; outer--)
{
int k = 0;
for( int j=1; j<54; j++ )
{
int temp[][] = new int[2][2];
if( j <= 52 )
{
//System.out.println(j + "Before\t" + index[j][0] + "\t" + index[j][1] + "\t" + index[j+1][1]);
if( index[j][0] > index[j + 1][0] )
{
temp[0][0] = index[j][0];
temp[0][1] = index[j][1];
index[j][0] = index[j + 1][0];
index[j][1] = index[j + 1][1];
index[j + 1][0] = temp[0][0];
index[j + 1][1] = temp[0][1];
//System.out.println(j + "after\t" + index[j][0] + "\t" + index[j][1] + "\t" + index[j+1][1]);
}
}
}
}
 
Michael Ernest
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Lou -
Good deal. As for better approaches, it depends on what you want. If your needs can be sized at compile time or to start out the program run, and you can quickly and effectively bang out the array code, then I don't see a problem with this approach. It's not "object-oriented" code, but clearly that's not a requirement, nor do I think it would be a good requirement to have in this case.
But let's say your priorities change, and you want code that's a easier to read, and the speed of array access, given the small size of your structures, isn't really a benefit. Or you need to apply this same strategy to a variety of applications -- turn it into library code, so to speak.
If that's the case, I might use some implementation of a List instead of arrays. List objects can be passed to the Collections helper class, which has -- ta da! -- a sort() method available. The learning curve is the cost. Worth considering, though, if you'll frequently be doing this kind of work.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A few other notes:
Like Collections, the Arrays class also has a sort() method available. You would need to define a Comparator which knows how to tell what order two elements should be in (as you would for the Collections method), but it's worth your time to learn how to do this. It takes some time and effort to write a good efficient sort routine, aspecially if you're new to it - the folks who put sort() together have done a good job with it, and it's likely to be notably faster than one you write yourself.
To swap two rows, you don't need to swap each value separately. You can swap the rows all at once:
<pre>
int[] temp = index[j];
index[j] = index[j + 1];
index[j + 1] = temp;
</pre>
Remember that array indices start at 0, not 1. And what happens when j = 53? Is there any reason to include this value in the loop at all?
reply
    Bookmark Topic Watch Topic
  • New Topic