FAQ: Merge Sort: Java - Merging

This community-built FAQ covers the “Merging” exercise from the lesson “Merge Sort: Java”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Pass the Technical Interview with Java

FAQs on the exercise Merging

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

The exercise uses a merge function that checks whether the current element in the left array is smaller than the current element in the right array, and puts the smallest of them into the merged array.
It checks the integer at each index.

But I thought it was only necessary to check the first element in each array (the stuff at index 0) for the merging in a “Merge Sort”.
I came up with a merge method that would do that:

    public int[] mergeByLowestFirstNum(int[] left, int[] right) {
      if ((right == null) || (right.length == 0)) {
        return left;
      }
      else if ((left == null) || (left.length == 0)) {
        return right;
      }
      int[] merged = new int[left.length + right.length];
      // switch left and right if first element of right is smallest
      if (right[0] < left[0]) { 
        int[] temp = left;
        left = right;
        right = temp;
      }
      
      int curIndex = 0;
      // put stuff from left into merged
      for (int leftPos = 0; leftPos < left.length; leftPos++) {
        merged[curIndex] = left[leftPos];
        curIndex++;
      }
      // put stuff from right into merged
      for (int rightPos = 0; rightPos < right.length; rightPos++) {
        merged[curIndex] = right[rightPos];
        curIndex++;
      }

      return merged;
    }

And I guess those for-loops could be replaced with

      System.arraycopy(left, 0, merged, 0, left.length);  // copy left into merged
      System.arraycopy(right, 0, merged, left.length, right.length);  // copy right into merged

I think that fails in some very specific cases, like for [5,4,3,2,1,0,6,8]