Thread: Sorting Multiple Parallel Arrays

1. Member Join Date
May 2010
Posts
11
Rep Power
0 Sorting Multiple Parallel Arrays

So it's end of the year finals time... My EOY the project is to input a file, do a bunch of calculations, sort redo the calculations, or I could do the calculations once and then sort the calculations along with the input. I think since I'm reading the file into an array anyway it's easy enough to print out line by line and do the calculations after each sort.

Ok so it's easy to iterate through the parallel arrays and display the results.

Java Code:
for ( i =0 ; i < names.length ; i++){
outFile.print(names[i] + " " + hours[i] + " " + payRate[i] + " ");
outFile.print(calcPay(hours[i], payRate[i]) + " " + calcTax(payRate[i]);
}
Where I am getting stuck is in the sort and print. ie I need to sort an alphabetical list of names and then print out the results sorted.
then i need to sort by hours and do the same thing. normally I would use:
Java Code:
Arrays.sort(names);
but this doesn't sort the other two arrays and I'm stuck with my names not matching pay rate etc.  Reply With Quote

2. You'd be better off not using parallel arrays, but rather create a class that holds all the information about a single person (name, pay, etc), and create objects for each person (employee?). If you want to make it even better, have the class implement the Comparable interface so that sorting an array of these objects is much easier.  Reply With Quote

3. Senior Member Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
10 If you simply must have your information in parallel arrays (Fubarables solution is more practical, and more in tune with OOP), then you'd need to write your own sorting algorithm (not very difficult), and do the same transformations on both arrays at the same time. An example with bubble sort:
Java Code:
int[] values = {...};
String[] names = {...};
void bubble() {
for(int i = 0; i < values.length; i++) {
for(int j = i; j < values.length; j++) {
if(values[i] > values[j]) {
swapValues(i, j);
swapNames(i, j);
}
}
}
}

void swapValues(int i, int j) {
int temp = values[i];
values[i] = values[j];
values[j] = temp;
}

void swapNames(int i, int j) {
String temp = names[i];
names[i] = names[j];
names[j] = temp;
}  Reply With Quote

4. Member Join Date
May 2010
Posts
11
Rep Power
0  Originally Posted by Fubarable You'd be better off not using parallel arrays, but rather create a class that holds all the information about a single person (name, pay, etc), and create objects for each person (employee?). If you want to make it even better, have the class implement the Comparable interface so that sorting an array of these objects is much easier.
while I agree with you entirely that's not the assignment. this is the teacher who wrote in the notes on my first assignment (it was a add two numbers from the command line type program, to which I used an array list and allowed the addition of multiple numbers) "You don't know how to use this. DO the assignment and turn in again."

however, I am curious about this "Comparable" interface. how would that be implemented? personally I would do something like:
Java Code:
public class employee{
private string _name;
private double _hoursWorked;
private double _payRate;

public static void employee(string name, double hours, double pay){
_name = name;
_hoursWorked = hours;
_payRate = pay;
}
public static string getName(){
return _name;
}
public static double getHours(){
return _hoursWorked;
}
public static double getPay(){
return _payRate;
}
} //end employee

public class final_project{
public static void main(){
...
Employee[] castList = new Employee[X];
for ( int i = 0; i < castList.length; i++){
StringTokenizer st = new StringTokenizer(line);
name = st.NextToken();
pay = st.NextToken();
hours = st.NextToken();
castList[i] = (name, pay, hours);
} //end for
}
so how would I sort my array of employees?

this is what I would think would be the best implementation. an array of a class.

then I could get any values I needed with:
Java Code:
...
local_name = castList.getName;
...
also can any method inside my final_project {} class access my employee{} class via the public methods? or do I have to pass my array of ojects?  Reply With Quote

5. Member Join Date
May 2010
Posts
11
Rep Power
0 so here is my take on the parallel swap with my modification for a 2d data array instead of a 1d array. also I passed the array into the method so the method could modify the array. otherwise the code wouldn't compile.

any thoughts?

Java Code:
int[] values = {...};
String[] names = {...};
void bubble() {
for(int i = 0; i < values.length; i++) {
for(int j = i; j < values.length; j++) {
if(values[i] > values[j]) {
swapValues(i, j, values);
swapNames(i, j, names);
}
}
}
}

void swapValues(int i, int j, int[][] values) {
int temp = values[i];
values[i] = values[j];
values[j] = temp;
int temp2 = values[i];
values[i] = values[j];
values[j] = temp;
}

void swapNames(int i, int j, string[] names) {
String temp = names[i];
names[i] = names[j];
names[j] = temp;
}  Reply With Quote

6.  Originally Posted by Pyrexkidd so here is my take on the parallel swap with my modification for a 2d data array instead of a 1d array. also I passed the array into the method so the method could modify the array. otherwise the code wouldn't compile.

any thoughts?
Yep, as soon as you run that code you'll get an ArrayIndexOutOfBoundsException. Your values[][] array has two columns. Array indexing starts at zero so the column indexes are 0 and 1.

kind regards,

Jos  Reply With Quote

7. Member Join Date
May 2010
Posts
11
Rep Power
0 Yep, as soon as you run that code you'll get an ArrayIndexOutOfBoundsException. Your values[][] array has two columns. Array indexing starts at zero so the column indexes are 0 and 1.

kind regards,

Jos
haha... shoulda seen that one coming... it's only funny cause it got me stuck for close to five minutes... if I've made that mistake once...

but how about implementing this code with objects rather than parallel arrays?

I can create objects using that class. but I'm not sure how to sort an array of objects.  Reply With Quote

8.  Originally Posted by Pyrexkidd I can create objects using that class. but I'm not sure how to sort an array of objects.
Suppose you have two objects, both instantiations of a same class T; also suppose you can say: a.comparteTo(b) and the result is negative if the relation a < b is true; the result is positive if the relation a > b is true and the result is zero if a == b is true. That is exaclty what the Comparable interface is about and it is possible to sort a list of Comparables because the relation defines an ordering. All you have to do is implement that interface and apply one of the ready made sorting algorithms (Collections, Arrays).

kind regards,

Jos  Reply With Quote

Tags for this Thread

arrays, parallel arrays, sorting  Posting Permissions

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