1

In my assignment the third step is to Call the method merge to merge the two lists in list1 so that the list1 remains sorted.

I write my code but it doesn't work well , the output show wrong because it important to be sorted

 public static void merge (ArrayList<Integer> list1, ArrayList<Integer> list2)
 {
        int i;
        int n=list1.size();
        int pos , j=0;

        for (pos =0 ;pos<n ; pos++)
        {
            for ( i=0 ; i<n ; i++)
                if (list1.get(j)>list2.get(pos))
                    list1.add(pos,list2.get(pos));
                else 
                    j++;
       } 
 }
5
  • 1
    en.wikipedia.org/wiki/Merge_sort Commented Oct 25, 2012 at 13:30
  • @Laf I didn't put the tag (homework ) . some one put it 4 me :) Commented Oct 25, 2012 at 13:32
  • @PaulRuane this is for array not arraylist , thanks Commented Oct 25, 2012 at 13:33
  • What do you mean? Merge Sort is an algorithm for ordered sequences, however they are represented. BTW an ArrayList is a variable length data-structure that wraps an array. Commented Oct 25, 2012 at 13:46
  • @PaulRuane I am still beginner in java , thanks alot for ur precious time :) Commented Oct 25, 2012 at 13:54

7 Answers 7

10

You only need one for loop assuming both lists are sorted:

public static void merge(List<Integer> l1, List<Integer> l2) {
    for (int index1 = 0, index2 = 0; index2 < l2.size(); index1++) {
        if (index1 == l1.size() || l1.get(index1) > l2.get(index2)) {
            l1.add(index1, l2.get(index2++));
        }
    }
}  

If l2 isn't sorted, you need two loops:

public static void merge(List<Integer> l1, List<Integer> l2) {
    for (int index2 = 0; index2 < l2.size(); index2++) {
        for (int index1 = 0; ; index1++) {
            if (index1 == l1.size() || l1.get(index1) > l2.get(index2)) {
                l1.add(index1, l2.get(index2));
                break;
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot , but sorry to bother you but the middle element in list2 doesn't sorted with them . for example : list1= 1 7 11 13 , list2 = 4 0 25 list1 after merge = 1 4 0 7 11 13 25
@rain: Added a variant for unsorted l2.
2

Easy fix: sort afterwards.

list1.addAll(list2);
Collections.sort(list1);

Use sets to avoid duplicates.

2 Comments

Works but may (sadly) not be what your assignment expects.
the doctor doesn't teach us this method , maybe he want us to use loops
1
public static void merge (ArrayList<Integer> list1, ArrayList<Integer> list2)
{
    list1.addAll(list2);
    Collections.sort(list1);
}

Comments

0

After you merge the lists, call sort method as below.

Collections.sort(list1); // carries out natural ordering.

If you need a customized sorting, use Comparator object

Collections.sort(list1, comparatorObject);

Check Comparator example for more details.

Here is your modified code:

 public static void merge (ArrayList<Integer> list1, ArrayList<Integer> list2)
 {
    list1.add(list2); //merges list 2 to list1
    Collections.sort(list1); //natural ordering
 }

Comments

0

If your input Lists aren't too long, I'd suggest just merging the lists and using the Collections.sort() Method to restore the order:

public static void mergeAndSort(List<Integer> list1, List<Integer> list2) {
    List<Integer> combinedList = new ArrayList<Integer>(list1);
    combinedList.addAll(list2);
    Collections.sort(combinedList);
    return combinedList;
}

As a sidenote, you should use the List interface instead of the ArrayList implementation class wherever possible.

Comments

0
public list mergeAndSort(List<integer> list1, List<integer> list2){
List<integer> list3;
int list2Size = list2.size();
for(int i=0;i<list2Size;i++){
    list1.add(list2(i));  
}
// Here we got all the elements in 1 list i.e list1

int list1Size = list1.size();
for(i=0;i<list1Size;i++){
    int small = 0;
    for(int j=i;j<list1size;j++){
        if(list1(i)> list2(j)){
            small = list2(j;
        }
    }
    list3.add(small); //Smallest 1 will be added to the new list
}

}

1 Comment

Congratulations on your first answer, @Satish. Your answer would be more beneficial to current and future answer seekers if you gave it a little more explanation. See How to Answer
-1
     ArrayList<Integer> a = new ArrayList();
     ArrayList<Integer> b = new ArrayList();
     ArrayList<Integer> c = new ArrayList();

     a.add(1);
     a.add(3);
     a.add(5);
     a.add(7);
     a.add(17);
     a.add(27);
     a.add(37);

     b.add(0); 
     b.add(2);
     b.add(4);

     while( a.size() > 0 || b.size() >0){

        if( a.size() == 0 || b.size() == 0){
            c.addAll(b);
            c.addAll(a);
            break;
        }

        if(a.get(0) < b.get(0)){
            c.add(a.get(0));
            a.remove(0);
        }
        else{
            c.add(b.get(0));
            b.remove(0);
        }

    }

    System.out.println(c.toString());

1 Comment

Generally, answers without explanations are considered not very useful. Please add an explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.