-1

I have two arrays:

  • int[] arr1
  • int[] arr2

Both the above have a size of n which is a user input.

I need to arrange arr2 in ascending order using java. How do i change the value of arr1 corresponding to the value of arr2?

Example(more info):

int[] arr1={1,2,3,4,5};
int[] arr2={3,6,5,1,9};
Arrays.sort(arr2);

How i want the output:

  • arr1={4,1,3,2,5}
  • arr2={1,3,5,6,9}

Here arr2 is already sorted

Nayantara Jeyaraj
  • 2,624
  • 7
  • 34
  • 63
Khushwanth
  • 103
  • 7
  • What do you mean by `How do i change the value of arr1 corresponding to the value of arr2`? You want `arr1` to be sorted? Or arranged in some particular order comparing with `arr2`. I reckon some information is missing. Could you please elaborate. And also, please provide what you've tried so far. – iMan Aug 24 '16 at 09:30
  • @iMan It could be better explained, but I think what is desired here is to rearrange both arrays equally, so `arr2` ends sorted. In the example, both have the 4th element first, 1st element second, etc. It is really a bit hard to explain. – Ray O'Kalahjan Aug 24 '16 at 09:32
  • KVilla, show us what you tried so far. You could try sorting arr2 by hand (instead of using that sort method) and then evertime you change something in arr2 you just have to make the same change in arr1. – Mark Aug 24 '16 at 09:34
  • @RayO'Kalahjan that makes sense. Lets see if that is what he indeed meant. :) – iMan Aug 24 '16 at 09:34
  • Please explain the logic behind what you want to do. It is unclear to me why 4 comes first in the sorted `arr1`. – fge Aug 24 '16 at 09:35
  • Is it that the same permutation has to be applied to arr1? So: In arr2 "1" is sorted from index 3 to index 0 , so in arr1 the value in index 3 shall also be transferred to index 0 ? – Fildor Aug 24 '16 at 09:41
  • @Fildor, yes, exactly. Like how in excel, we sort data of one column and the other columns also change their order. The arrays are related, so change of index of one must also change the position/index of the other. – Khushwanth Aug 24 '16 at 13:37

2 Answers2

0

If you have no duplicate numbers in the second array like the provided example, you can use Map.

At first, take a map. Put array2 values into key and array1 values into value in the map. Then sort the map according to the values. Now if you get the keys from the map, you'll get what you want.

Use TreeMap. It doesn't need sorting. It enters data such way that guarantees that the map will be in ascending key order.

Map<Integer, Integer> m = new TreeMap<>();

int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = {3, 6, 5, 1, 9};

// Put array2 values as key parameter and array1 values as value parameter
for (int i = 0; i < arr1.length; i++) {
    m.put(arr2[i], arr1[i]);
}

int i = 0;

// Print the sorted map
for (Map.Entry<Integer, Integer> entry : m.entrySet()) {
    System.out.println("arr1["+ i +"] = " + entry.getValue());
    i++;
}

Output:

arr1[0] = 4
arr1[1] = 1
arr1[2] = 3
arr1[3] = 2
arr1[4] = 5
Shahid
  • 2,288
  • 1
  • 14
  • 24
0

What I would do is the following:

Create a class that contains two numbers (one from arr1 and another from arr2). Make it implement comparable and compare the second number.

public class Pair implements Comparable<Pair> {

    private Integer e1;
    private Integer e2;

    @Override
    public int compareTo(Pair o) {
        return getE1().compareTo(o.getE1());
    }
}

In the method, make a list of Pair and fill it with the elements of both arrays, then simply sort it with the method in Collections.

int[] arr1={1,2,3,4,5};
int[] arr2={3,6,5,1,9};
ArrayList<Pair> list = new ArrayList<Pair>();
for(int i = 0; i < arr1.length; i++) {
    list.add(new Pair(arr1[i], arr2[i]);
}
Collections.sort(list);