I have two ArrayList's, for example:
ArrayList a = new ArrayList();
a.add(10);
a.add(35);
a.add(51);
ArrayList b = new ArrayList();
b.add(24);
b.add(46);
b.add(81);
I need to create a function, which puts elements from B to A in a sorted query. (In my mind it must check elements on the same positions in A and B and put 24 between 10 and 35, 46 between 35 and 51, and the last one is 81). I have:
public static void merge(ArrayList a, ArrayList b)
{
a.addAll(b);
Collections.sort(a);
}
But it is not an efficient algorithm (N^2). Is there a more efficient one?