I need help troubleshooting my code for MergeSort and Merge

merge sort
merge sort pseudocode
merge sort code
quicksort
merge sort python
insertion sort
merge sort algorithm with example
merge sort example

So I've been working on MergeSort for an Algorithm project, but I've ran into various problems when it comes to getting the code to sort the arrays. Whenever I generate a string and put it into MergeSort, it seems to just come out exactly the same. I want some help in finding where the mistake in my code is, why is it giving me this, and a solution with a simple, but good explanation.

Here's what I've tried in the past:

  1. I've tried to use return arr[0] instead of arr, but it throws me an error with it being unable to convert from int to int[].
  2. I've looked in my merge class and everything seems to be ok there.
  3. I've discussed the issue with my teacher and he says that everything looks fine, but I know that there must be something wrong somewhere.
  4. I've tried to remove return merge(arr1, arr2) but the system throws an error telling me I have to return something.
  5. I've tried to print out the arrays individually, but it still shows no changes and is the exact same as the original string.

Merge method:

private static int[] merge(int[] a, int[] b)
{
    int[] c = new int[a.length + b.length];
    int counterA = 0;
    int counterB = 0;
    int counterC = 0;

    while (counterA != a.length && counterB != b.length)
    {
      if (a[counterA] < b[counterB])
      {
        c[counterC] = a[counterA];
        counterA++;
        counterC++;
      }
      else
      {
        c[counterC] = b[counterB];
        counterB++;
        counterC++;
      }
    }

    while (counterB == b.length && counterA != a.length)
    {
      c[counterC] = a[counterA];
      counterA++;
      counterC++;
    }

    while (counterA == a.length && counterB != b.length)
    {
      c[counterC] = b[counterB];
      counterB++;
      counterC++;
    }

    return c;
}

MergeSort method:

public static int[] mergeSort(int[] arr)
{ 
    if (arr.length == 1)
    {
      return arr[0];
    }

    int[] arr1 = new int[arr.length / 2];
    int[] arr2 = new int[arr.length - arr1.length];

    for (int i = 0; i < arr1.length; i++)
    {
      arr1[i] = arr[i];
    }

    for (int i = 0; i < arr2.length; i++)
    {
      arr2[i] = arr[i + arr1.length];
    }

    arr1 = mergeSort(arr1);
    arr2 = mergeSort(arr2);

    return merge(arr1, arr2);
}

Because the array is randomly generated, an example would be this:

9, 1, 7, 5, 7, 2, 2, 9, 8, 9

The intended result should be this:

1, 2, 2, 5, 7, 7, 8, 9, 9, 9

However, this is what the output is instead (The array comes out unchanged):

9, 1, 7, 5, 7, 2, 2, 9, 8, 9 

6.11. The Merge Sort — Problem Solving with Algorithms and Data , Merge sort is a recursive algorithm that continually splits a list in half. function, we need to consider the two distinct processes that make up its implementation. Solve practice problems for Merge Sort to test your programming skills. Also go through detailed tutorials to improve your understanding to the topic.

The only problem I see in the code is the return arr[0]; instead of return arr; or return new int[]{arr[0]}; in mergeSort.

Aside from that the code works as expected.

If you are not seeing the correct output, you probably aren't using the output correctly.

Here is an example of how I tested it.

    public static void main(String[] args) {

        int[] input = new int[]{9, 1, 7, 5, 7, 2, 2, 9, 8, 9};
        int[] output = mergeSort(input);
    }

Merge sort algorithm overview (article), Because we're using divide-and-conquer to sort, we need to decide what our subproblems are going to look like. The full problem is to sort an entire array. In the next challenge, you'll focus on implementing the overall merge sort algorithm, Why is that happen while I dont see any code to do that after the merge? Please� How to troubleshoot performance problems A merge replication application must provide the functionality required by the business rules of the application and also complete its operations in a timely manner, even if the volume of data and the number of users increase over time.

Your code doesn't compile as written. In mergeSort, if the array length is equal to 1, it should return arr. After this change, your code works fine.

We can, however, make a few small changes to your code to make it cleaner. Notice that for your last while loop in merge, the check counterA == a.length will always be true. Therefore, we can remove it. Also, incrementing counters can be done while accessing the array. Combining these suggestions results in the following code for your merge method:

private static int[] merge(int[] a, int[] b)
{
    int[] c = new int[a.length + b.length];
    int counterA = 0;
    int counterB = 0;
    int counterC = 0;

    while(counterA != a.length && counterB != b.length)
    {
        if(a[counterA] < b[counterB])
            c[counterC++] = a[counterA++];
        else
            c[counterC++] = b[counterB++];
    }

    while(counterB == b.length && counterA != a.length)
        c[counterC++] = a[counterA++];

    while(counterB != b.length)
        c[counterC++] = b[counterB++];

    return c;
}

Merge Sort Tutorials & Notes | Algorithms, Backtracking further, we then need to merge the list consisting of element ( 8 ) too with this list, leading to the new sorted list ( 7 , 8 , 9 ) . An implementation has� Algorithm for Merge Sort: /* Double-Click To Select Code */ function merge_sort(list m) // if list size is 0 (empty) or 1, consider it sorted and return it // (using less than or equal prevents infinite recursion for a zero length m) if length(m) <= 1 return m // else list size is > 1, so split the list into two sublists var list left, right var integer middle = length(m) / 2 for each x in m before middle add x to left for each x in m after or equal middle add x to right // recursively call

Merge Sort, Like QuickSort , Merge Sort is a Divide and Conquer algorithm. It divides input array See following C implementation for details. MergeSort(arr[], l, r) If Recommended: Please solve it on “PRACTICE” first, before moving on to the solution. C/C++; Java Inversion Count Problem; Used in External Sorting� The following diagram shows the complete merge sort process for an example array {10, 6, 8, 5, 7, 3, 4}. If we take a closer look at the diagram, we can see that the array is recursively divided into two halves until the size becomes 1. Once the size becomes 1, the merge processes come into action and start merging arrays back while sorting: 3.

Problem - 873D, Merge sort is a well-known sorting algorithm. The main function The array in this problem is 0-indexed, so to sort the whole array, you need to call mergesort(a , 0, n). Help Ivan to find an array he wants! Input Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. Therefore merge operation of merge sort can be implemented without extra space for linked lists. In arrays, we can do random access as elements are contiguous in memory. Let us say we have an integer (4-byte) array A and let the address of A[0] be x then to access A[i], we can directly access the memory at (x + i*4).

Merge Sort In C++ With Examples, In this tutorial, we will learn more about Merge sort which uses the Merge sort algorithm uses the “divide and conquer” strategy wherein we divide the problem into As shown in the above pseudo code, in merge sort algorithm we Next to find the middle element of the array we require single step i.e.� Detailed tutorial on Merge Sort to improve your understanding of {{ track }}. Also try practice problems to test & improve your skill level.

Comments
  • Start shorter, and start "not random". You're still in develop stages, so: public static int[] mergeSort(), and then make the method itself start with int[] arr = {9, 1}, and then start stepping through your code: what should happen at each line? There's only 2 elements to sort, so it should be very easy to go "at line X, the variables should point at things that look like Y". And then see if you're right.