Sorting a 2-Dim array.

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

Sorting a 2-Dim array.

Postby soldierforce » Tue Apr 07, 2009 2:58 pm

Hi,

Can someone tell me how I can sort a two dimensional array according to lowest to highest of the first column only?

What I mean is I have a 2 dimensional array with the X and Y values. I then need to sort it by the X values only and the Y values should be in the same row as the X value before sorting.

Like

X Y
3 18
4 45
1 32
2 99

after sorting by the X column the values should read as:

X Y
1 32
2 99
3 18
4 45
soldierforce
New User
 
Posts: 15
Joined: Thu Oct 23, 2008 4:52 pm

Postby lawrie » Tue Apr 07, 2009 9:27 pm

Here is a solution using a bubble sort:

Code: Select all
import lejos.nxt.Button;

public class BubbleSort {

  public static void main(String[] args) {
    int[][] a = {{3,18}, {4,45}, {1, 32}, {2,99}};
   
    bubbleSort(a);
   
    for(int i = 0;i<a.length;i++) {
      String s = "";
      for(int j=0;j<a[i].length;j++) s += a[i][j] + " ";
      System.out.println(s);
    }
   
    Button.waitForPress();
  }
 
  public static void bubbleSort(int[][] a) {
    while(true) {
      boolean swapped = false;
      for(int i=0;i<a.length-1;i++) {
        if (a[i][0] > a[i+1][0]) {
          swapped = true;
          for(int j=0;j<a[i].length;j++) {
            int t = a[i][j];
            a[i][j] = a[i+1][j];
            a[i+1][j] = t;
          }
        }
      }
      if (!swapped) return;
    }
  }
}
lawrie
leJOS Team Member
 
Posts: 934
Joined: Mon Feb 05, 2007 1:27 pm

Postby soldierforce » Tue Apr 07, 2009 11:26 pm

Thanks Lawrie. But I have another problem. I am using a sample application where there are two arrays not 2-dim. I now want to create a 2-dim and add the two together to form the new 2-dim one. How can I do that?

It might look stupid but this is what I have been trying:

int[] firstarray = new int [10];
int[] secondarray = new int [10];

int [][] newarray = new int [2][10];

newarray[0] = firstarray;
newarray[1] = secondarray;

I haven't done this in java before and looking online I thought this was the way to do it.
soldierforce
New User
 
Posts: 15
Joined: Thu Oct 23, 2008 4:52 pm

Postby soldierforce » Wed Apr 08, 2009 11:54 am

or is there any sort which can be done on two seperate arrays? that the second array is sorted according to elements in the first? What I am trying to do is use the NXT with a light sensor and 2 servo motors. In one array I save the light sensor values and in the other the distance. Its in a straight line so the distance is already sorted what I need to do is sort the light sensor values from 0-100. I only started working with arrays last week. I just want to know how to do things, like we couldn't return 2 values so to create an array and return the array with 2 values etc. So far I am fine only getting stuck at the 2-dim sorting or sorting 2 different arrays thing.
I somehow find Java easier to learn with the help of the NXT :? maybe cos its fun...
soldierforce
New User
 
Posts: 15
Joined: Thu Oct 23, 2008 4:52 pm

Postby lawrie » Fri Apr 10, 2009 8:21 pm

int[] firstarray = new int [10];
int[] secondarray = new int [10];

int [][] newarray = new int [2][10];

newarray[0] = firstarray;
newarray[1] = secondarray;


You can do this, but it is more efficient to do:

Code: Select all
int[] firstarray = new int [10];
int[] secondarray = new int [10];

int [][] newarray = new int [2][];

newarray[0] = firstarray;
newarray[1] = secondarray;


or

Code: Select all
int [][] newarray = {firstarray, secondarray};


However, I am not sure that what you want is a [2][10] array, but a [10][2] array, so that you can return the 2-element array that contains the light value and distance.

It is possible to sort two separate arrays in a similar way to a 2-dimensional array, if that is what you want to do.

The best solution to what you are trying to do is probably to define a class that contains the light value and distance and to define a compareTo method on that class. You can then sort a one dimensional arrays of the objects in your new class.
lawrie
leJOS Team Member
 
Posts: 934
Joined: Mon Feb 05, 2007 1:27 pm

Postby soldierforce » Fri Apr 10, 2009 10:25 pm

I already have a class that stores the light values and distance in an array.

what is the compareTo method? I googled to check it and got "Compares the current instance with another object of the same type". How does this help me sort the two arrays? do you mean the reference of first element in the sorted array compared to the unsorted array? and it will return the position of the element in the second array?
soldierforce
New User
 
Posts: 15
Joined: Thu Oct 23, 2008 4:52 pm

Postby lawrie » Sat Apr 11, 2009 10:00 am

I was suggesting that you have a class that stores a single light sensor and distance value, not an array of them. That is the best way to hold multiple related values.

compareTo is a method from the Java Comparable interface, which we will support in the next version of leJOS. If you implement a compareTo method on your class you will be able to use sort methods such as Arrays.sort when we introduce them into lejOS in a future release.

You class will be something like:

Code: Select all
/**
 * Holds the value of a light sensor reading at a distance along a line.
 */
public class LightValue {
  public int value;
  public int distance;

  public LightValue(int value, int distance) {
    this.value = value;
    this.distance = distance;
  }

  public int compareTo(LightValue lv) {
    if (value < lv.value) return -1;
    else if (value > lv.value) return 1;
    else return 0;
  }
}
 


You can then hold LightValue objects in an array and sort them using the compareTo method. With this solution you need a single one-dimensional array of lightValue objects and you can change the bubbleSort method to work on this array using the compareTo method. Using compareTo does not help you much with the current version of leJOS, but it is the correct way to do this in Java and will make things much easier in future versions of leJOS.
lawrie
leJOS Team Member
 
Posts: 934
Joined: Mon Feb 05, 2007 1:27 pm

Postby soldierforce » Sat Apr 11, 2009 11:05 am

lawrie, thanks alot for replying to each and every query of mine!

But I have still not understood if I am reading just one value how do I populate the array? Is it something like I create another class for the array and then keep calling the LightValue class with a loop to populate the array? This is more confusing than what I thought in the beginning! :shock: I thought all I had to do is create two arrays and sort them then create a loop to read from the first to the last element in the array!
soldierforce
New User
 
Posts: 15
Joined: Thu Oct 23, 2008 4:52 pm

Postby lawrie » Sat Apr 11, 2009 8:27 pm

You can use two arrays if you like. All you need to do is move the corresponding elements in the second array as you sort the first one.

What I proposed is a more object-oriented way of doing it. You can hold your array in your main class or any other class, but not in the LightValue class. As you read light sensor values, you create a new LightValue object and insert it into your array. You can then sort the array.
lawrie
leJOS Team Member
 
Posts: 934
Joined: Mon Feb 05, 2007 1:27 pm

Postby soldierforce » Sat Apr 11, 2009 10:43 pm

Finally I think I have understood what you meant! That is to sort the array according to every new value read? What I am doing is just create the array first. Next step is sort that according to Light Values from 0-100.

I think I will create a new 2-dim array (SortedArray) of the two single arrays (LightValues, Distances). And then bubble sort the 2-dim array.

Do let me know if you think I am have the wrong idea or doing something wrong! :wink:

Thanks again for all your help!

Happy Easter to all...
soldierforce
New User
 
Posts: 15
Joined: Thu Oct 23, 2008 4:52 pm


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 4 guests

more stuff