0

Im trying to learn java by myself and one of the questions in the book is handing my but to me. It's about merging two ArrayList after sorting them MergeSort style. I can't merge them back together without a major calamity. I really want to know it so I could move on, it's driving me nuts.

 public <E extends Comparable<?super E>> void merge(List<E> front, List<E> back, boolean last, List<E> result){
  int i = 0;
  int j = 1;
    while (!front.isEmpty() && !back.isEmpty()) {
      if (front.get(0).compareTo(back.get(0)) <= 0) {
            result.add(result.size() -1, front.remove(0));
            System.out.println("works" + result);
          } else {
            result.add(result.size() -1, back.remove(0));
            System.out.println("no work" + result);
          }
    }
     while (!front.isEmpty()) {
        result.add(result.size() -1,front.remove(0));
    }
    while (!back.isEmpty()) {
        result.add(result.size() - 1, back.remove(0));
    }
System.out.println();
} } 

The boolean value is to sort them in: true==ascending order, false==descending order. I could worry about that. Any type of help will be appreciated.

IC2D
  • 471
  • 4
  • 11
  • 22
  • 2
    *"... and one of the questions in the book is handing my but to me"*. What language is this? It doesn't look like English. (Please remember that there are people over the age of 21 reading this, and people whose first language is not English.) – Stephen C Mar 21 '12 at 01:16
  • 2
    What do you mean by "major calamity"? What, specifically, is going wrong? Does it throw an exception? Does it not compile? Does it return an unexpected result? – Cameron Skinner Mar 21 '12 at 01:20

3 Answers3

1

I think all you have to do is make the following change

result.add(front.remove(0));

and the same change in the else clause.

To add an element at the end of the result list you shouldn't specify an index.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
0

If your arraylists are sorted, loop through them and just take the minimum element at one end (assuming you want ascending order). Do this until one of the lists is empty. Then just append the entries in the leftover list to the merged list.

Adrian
  • 5,603
  • 8
  • 53
  • 85
  • you'll have to excuse me, I still don't understand. – IC2D Mar 21 '12 at 01:50
  • @user1217522less see http://www.algolist.net/Algorithms/Merge/Sorted_arrays and http://stackoverflow.com/questions/5958169/how-to-merge-two-sorted-arrays-into-a-sorted-array – Adrian Mar 21 '12 at 13:17
0

Well, one safe method I used to implement the merging was something like the following.

public <E extends Comparable<? super E>> void merge(List<E> front,
            List<E> back, boolean last, List<E> result) {
        int i = 0;
        int j = 0;
        Collections.sort(front);
        Collections.sort(back);
        while (!(i == front.size() && j == back.size())) {
            E e;
            if (i == front.size()) {
                // If we have exhausted the 'front' then copy whatever is in
                // 'back'
                e = back.get(j);
                // Move back index to next
                j++;
            } else if (j == back.size()) {
                // If we have exhausted the 'back' then copy whatever is in
                // 'front'
                e = front.get(i);
                // Move front index to next
                i++;
            } else {
                if ((front.get(i).compareTo(back.get(j)) <= 0)) {
                    // front item will be added, increment front index for next
                    e = front.get(i);
                    i++;
                } else {
                    // back item will be added, increment back index for next
                    e = back.get(j);
                    j++;
                }
            }
            result.add(e);
        }
        if(last){
            Collections.reverse(result);
        }
    }
prajeesh kumar
  • 1,916
  • 1
  • 17
  • 17