linking arrays

i have a quick question, if i have two arrays, say int [] data and int []x
int data : 0 4 5 1 int x : A B C D
and i want to sort data, but when it does, i want x to also sort, ( hard to explain but 0 is linked with A, (4, B) (5, C) and (1, D)

so if i sort data:
0
1
4
5

x should show:
A
D
B
C

is this the correct code for doing that?

public static void bubbleSort(int[] data, int [] x, int n)
{
for(int i = 0; i < n-1; i++)
{
for(int j = n-2; j >= i; j--)
{
if(data[j+1] < data[j])
swap(data, j, j+1);
else
return x[j];

}
}
}
[741 byte] By [rhinomist] at [2007-11-20 11:27:22]
# 1 Re: linking arrays
is this the correct code for doing that?

Here's an algorithm for determining if your code is correct:

1-Does it compile? Yes-proceed. No-not correct.
2-Does it run without exceptions when fed reasonable data? Yes-proceed. No-not correct.
3-Does it do what you expect? Yes-might be correct. No-not correct.

Your code wouldnt compile so it's not correct. You return something from a void function. Even if you fixed that, its obviously not correct. The bubblesort function is supposed to sort the arrays you feed it. The only thing I see you doing with the 'x' array is returning an element from it.

I'd suggest breaking your problem down into simpler problems. First get your bubblesort to sort just data & thats it. Once you've got that working, get it to also change x.
Martin O at 2007-11-10 2:14:05 >
# 2 Re: linking arrays
i have a quick question, if i have two arrays, say int [] data and int []x
int data : 0 4 5 1 int x : A B C D
and i want to sort data, but when it does, i want x to also sort, ( hard to explain but 0 is linked with A, (4, B) (5, C) and (1, D)

I'm not sure what A, B, C, and D are, but they don't look like ints, or maybe I'm mistaken. Since the int variables are innately tied together with the letter variables, it would make the greatest sense to me to put them in one class, make that class implement the Comparable interface and then sort a single array of objects of this class:

import java.util.Arrays;

public class IntLetter implements Comparable<IntLetter>
{
private int intVar;
private Object letterVar; // I have no idea what type of object or primative this should be.

public IntLetter(int intVar, Object letterVar)
{
this.intVar = intVar;
this.letterVar = letterVar;
}

public int getIntVar()
{
return intVar;
}

public Object getLetterVar()
{
return letterVar;
}

public int compareTo(IntLetter otherIntLetter)
{
return intVar - otherIntLetter.intVar;
}

public String toString()
{
return String.valueOf(intVar) + " - " + String.valueOf(letterVar);
}

public static void main(String[] args)
{
int[] ints = {0, 4, 5, 1 };
Object[] letters = {"A", "B", "C", "D"};
IntLetter[] intLetterArray = new IntLetter[ints.length];
for (int i = 0; i < intLetterArray.length; i++)
{
intLetterArray[i] = new IntLetter(ints[i], letters[i]);
}

Arrays.sort(intLetterArray);
System.out.println(Arrays.toString(intLetterArray));
}

}


You know what? Scrap all this. Probably better is to use a map.
petes1234 at 2007-11-10 2:15:05 >
# 3 Re: linking arrays
what the ABCD stood for is a town, and the ints for populations, but i just shortened towns to letters.

my code couldn't compile, but that was my fault, i didn't debug enough and now am sorting through a ton of code which could be wrong. what is the map that you were talking about pete?
rhinomist at 2007-11-10 2:16:04 >
# 4 Re: linking arrays
actually, A stood for one town, B for another. I just wanted to sort the populations out in increasing order, and have the towns changed in their array to reflect the population changes.
rhinomist at 2007-11-10 2:17:11 >
# 5 Re: linking arrays
1. Create a Town class that holds the town's data like the town name, population etc.
2. Create a Town object for each of your towns and store them all in a collection List class such as an ArrayList.
3. Then when you want to order the list call the Collections.sort(..) method passing in your list and a comparator that orders the towns as you require. This way you can change the list order to ascending/descending population size, alphabetical town name or whatever other order you want.
keang at 2007-11-10 2:18:10 >
# 6 Re: linking arrays
hey, i went to my TA and she said that we're not allowed to use the sort that java has, like array.sort, so that is why i have the 'i' counter and the 'j' counter, but i'm working off what you put up there pete, i'll just translate that into my code...thanks
rhinomist at 2007-11-10 2:19:12 >