0

I'm trying to implement this mergeSort() method without a return, but it fails to store the variable to the calling method. For example, when I call mergeSort(head) on the first head: {3, 2}, the method will print {2, 3} meaning list actually is sorted correctly, but in the method that called mergeSort(), head remains {3, 2}. Is there any way in which I can update the variable in the calling class as well?

Comments on the algorithm itself are appreciated as well, but please keep those in the comment section.

  @Test
    public void testMergesortUnsortedList() {
        List<Integer> sequence = new ArrayList<>(Arrays.asList(3, 2, 1, 5, 4));
        MergeSort.mergeSort(sequence);
        System.out.println("final: " + sequence);
        assertEquals(Arrays.asList(1, 2, 3, 4, 5), sequence);

        sequence = new ArrayList<>(Arrays.asList(3, 2, 1, 6, 5, 4));
        MergeSort.mergeSort(sequence);
        assertEquals(sequence, Arrays.asList(1, 2, 3, 4, 5, 6));
    }

    import java.util.ArrayList;
    import java.util.List;

    public class MergeSort {

        public static <E extends Comparable<E>> void mergeSort(List<E> list) {
            if (list.size() > 1) {
                // the first half of the list (always smaller than or equal to tail)
                ArrayList<E> head = new ArrayList<>(list.subList(0, list.size() / 2));
                // the second half of the list
                ArrayList<E> tail = new ArrayList<>(list.subList(list.size() / 2, list.size()));
                //temporary list
                ArrayList<E> temp = new ArrayList<>();

                if (head.size() > 1) {
                    mergeSort(head);
                }
                if (tail.size() > 1) {
                    mergeSort(tail);
                }

                // while either head or tail still has a member
                while (head.size() > 0 || tail.size() > 0) {
                    // if both head and tail still have members
                    if (head.size() > 0 && tail.size() > 0) {
                        // if head is smaller than tail
                        if (head.get(0).compareTo(tail.get(0)) < 1) {
                            temp.add(head.get(0));
                            head.remove(0);
                        } else {
                            temp.add(tail.get(0));
                            tail.remove(0);
                        }
                        // if only head has members
                    } else if (head.size() > 0) {
                        temp.add(head.get(0));
                        head.remove(0);
                        // if only tail has members
                    } else {
                        temp.add(tail.get(0));
                        tail.remove(0);
                    }
                }

                // overwrite the old list with the sorted list
                list = temp;
                System.out.println(list);
            }
        }

    }
NinjaDeveloper
  • 1,620
  • 3
  • 19
  • 51
HYBR1D
  • 464
  • 2
  • 6
  • 19
  • 1
    This question is by no means an exact duplicate of the one that was marked as such. True that it's related with it. – Andres Dec 21 '17 at 15:18

1 Answers1

0
list = temp;

this line is making the list argument in the method point to a different object than the list you passed as an argument.

As the comment below says:

Instead of reasigning list=temp which creates a new entity. copy the contents of temp to list. list.clear(); Collections.copyList(list,temp)
Andres
  • 10,561
  • 4
  • 45
  • 63
  • Instead of reasigning `list=temp` which creates a new entity. copy the contents of temp to list. list.clear(); Collections.copyList(list, temp); See : http://www.codejava.net/java-core/collections/java-list-collection-tutorial-and-examples for examples. – Dragonthoughts Dec 21 '17 at 15:17
  • @Andres Probably the lack of a solution – HYBR1D Dec 21 '17 at 15:40