Parth
Parth

Reputation: 265

How to connect two different arrays?

If I have one array of names, and one array of lap times, how would I link the two arrays so I can sort the times and still keep the associated names? this is what the two arrays look like:

String[] names = { "Paul", "Dan", "Hayden", "Sam", "Pauline"};
    int[] times = { 341, 273, 278, 329, 445 };

As of now, I run a selection sort algorithm to get lowest times to highest but i cant figure out how to keep paul connected to 341.

I do NOT want to concatenate the two arrays, I want to be able to sort the times array and then call the name associated with the particular time.

Upvotes: 1

Views: 401

Answers (3)

crchurchey
crchurchey

Reputation: 188

Leave the names and times arrays as-is and use a third array that contains indexes into the other two:

 String[] names = { "Paul", "Dan", "Hayden", "Sam", "Pauline"};
    int[] times = { 341, 273, 278, 329, 445 };
    int[] index = { 0, 1, 2, 3, 4};

Run your sort algorithm on names or times (depending on how you want to sort). When you run the algorithm, access the times array (for example) via the index array:

times[index[x]]; // where x is whatever index you are on during your sort

When you find you need to swap two items, do the swap operation on the index array:

swap(index[0], index[1]);

Once the sorting algorithm is completed, the names and times arrays remain associated via their array positions but if you access them via the index array they will be accessed in sorted order.

p.s. I feel dirty doing your school work for you :)

Upvotes: 0

Ankush soni
Ankush soni

Reputation: 1469

Sort the time array and swap position of element in both the array together, in that way when ever swap happens it will maintain the link between times and names array, here is the program for that, I did sorting in descending Order

public static void main(String[] args){
        String[] names = { "Paul", "Dan", "Hayden", "Sam", "Pauline"};
        int[] times = { 341, 273, 278, 329, 445 };
        
        
        for(int outerIndex = 0; outerIndex < times.length; outerIndex++){
            for(int innerIndex = outerIndex+1;  innerIndex < times.length; innerIndex++){
                if(times[outerIndex]<times[innerIndex]){
                    swap(outerIndex, innerIndex, names, times);
                }
            }
        }
    }

Here I am swaping element position in both the arrays:

public static void swap(int outerIndex, int innerIndex, String[] names, int[] times){
        int tempTime;
        String tempName;
        
        tempTime = times[outerIndex];
        tempName = names[outerIndex];
        
        times[outerIndex] = times[innerIndex];
        names[outerIndex] = names[innerIndex];
        
        times[innerIndex] = tempTime;
        names[innerIndex] = tempName;
    }

Input:

String[] names = { "Paul", "Dan", "Hayden", "Sam", "Pauline"};

int[] times = { 341, 273, 278, 329, 445 };

Output:

String[] names = {"Pauline", "Paul", "Sam", "Hayden", "Dan"};

int[] times = { 445, 341, 329, 278, 273};

Upvotes: 1

Pune
Pune

Reputation: 91

Please sort with array room. for example

names[0] = "Paul"
times [0] = 341

something like that

Upvotes: 0

Related Questions