lchristina26
lchristina26

Reputation: 205

How to merge two arraylists into one in ascending order

I need to merge two lists into one, in ascending order, not duplicates, and I think my code is really close, I'm just missing something and I can't figure it out. As of now, my code is not working properly in my merge method. I think it has something to do with my loops, but I just can't work around it. My current method prints the new list, but it is not in perfect increasing order. I would appreciate any assistance in figuring out how to make this method print my merged list with ascending order using the contents of l1 and l2.

**Note: I cannot use any built-in array sorting methods.

Thanks!

import java.util.ArrayList;
import java.util.Random;

public class MergeLists {

    public static ArrayList<Integer> merge(ArrayList<Integer> l1, ArrayList<Integer> l2){        
    ArrayList<Integer> mergedList = new ArrayList();
    for (int j = 0; j < l1.size(); j++) {
        if (l1.get(j) < l2.get(j)) {
            mergedList.add(l1.get(j));
            mergedList.add(l2.get(j));
        } else {
            mergedList.add(l2.get(j));
            mergedList.add(l1.get(j));
        }
    }
    for (int i = l2.size() - l1.size(); i < l2.size(); i++) {
        mergedList.add(l2.get(i));
    }
    return mergedList;
}

public static ArrayList<Integer> makeRandomIncreasingList(int length) {
    ArrayList<Integer> randomList = new ArrayList();
    Random rand = new Random();
    int inList = rand.nextInt(9) + 1;
    int inList2 = rand.nextInt(9) + 1;
    for (int i = 0; i < length; i++) {
        randomList.add(inList);
        inList = inList + inList2;
    }
    return randomList;
}

public static void doMergeTest() {
    ArrayList<Integer> list1 = makeRandomIncreasingList(10);
    ArrayList<Integer> list2 = makeRandomIncreasingList(20);
    ArrayList<Integer> mergedList = merge(list1, list2);
    System.out.println("List 1:" + list1);
    System.out.println("List 2:" + list2);
    System.out.println("Merged list:" + mergedList);
}

public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
        System.out.println("Performing merge test #" + (i + 1) + ":");
        doMergeTest();
    }
}
}

Upvotes: 1

Views: 4021

Answers (3)

webuster
webuster

Reputation: 2510

The first part of your merge() method seems ok, if you modify it a little bit. You need to be going through both lists in parallel, something like

int i = 0, j = 0;
for (; i < l1.size() && j < l2.size();)

And compare individual items and increment indices independently, as in

if (l1.get(i) < l2.get(j)) {
    ...
    i++;
} else
    ...
    j++;
}

The way you were doing it you were literally going in parallel, which is not always correct (think of lists [1 2 2] and [1 1 1] => your merge would look like [1 1 1 2 1 2])

Then, after your "parallel" for-loop (the one where you're iterating through both lists), one of your indices is always going to break your loop because it's at the end of its list. For in-order merging, I usually declare i, j outside the loop (you'll need then after your first for-loop, like above) and then do something like (in your notation):

for (int i1 = i; i1 < l1.size(); i1++) {
    mergeList.add(l1.get(i1));
}

for (int i2 = j; i2 < l2.size(); i2++) {
    mergeList.add(l2.get(i2));
}

After your first for-loop, you get to the end of exactly one of the lists (someone's going to break the loop), so exactly one of the above loops is going to get executed, and that will contain the remaining items, in order.

Edit: your last for-loop of the merge() method is not correct for your purpose.

Upvotes: 0

z atef
z atef

Reputation: 7679

Remove duplicates

arrayList1.remove(arrayList2);

Then merge two arrayList:

arrayList1.addAll(arrayList2);

And Lastly sort the last

collections.sort(arrayList1);

Another way is to use SET: Set doesnt allow duplicates
(HashSet is faster depending on the List implementation class)

Set setmerge = new HashSet(list1);

setmerge.addAll(list2);

list1.clear();

list1.addAll(setmerge);

Upvotes: 1

Ali Alavi
Ali Alavi

Reputation: 2467

You have assumed l2 items are always bigger than l1 items, since you are adding remainder of l2 items in the end of the list. You need to compare them with mergedList items and add them accordingly.

Upvotes: 0

Related Questions