Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7647887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:34:26+00:00 2026-05-31T10:34:26+00:00

Our teacher gave us the pseudocode for Merge Sort as below I want to

  • 0

Our teacher gave us the pseudocode for Merge Sort as below

enter image description here

I want to implement it in Java. My code is below:

public class MergeSorter {
/**
 * @param anArray
*/
    public MergeSorter(int[] anArray,int low, int high)
    {
        a = anArray;
        p = low;
        r = high;
    }
    public void sort()
    {
        if(p < r)
        {
           q = (p + r)/2;
           MergeSorter pqSorter = new MergeSorter(a, p, q);
           MergeSorter qrSorter = new MergeSorter(a, q + 1, r);
           pqSorter.sort();
           qrSorter.sort();
           merge(p, q, r);
        }
   }
private void merge(int low, int mid, int high)
{
   p = low;
   q = mid;
   r = high;
   int i;
   int j;
   int n1 = (q - p) + 1;
   int n2 = (r - q);
   int[] L = new int[n1+1];
   for (i = 0; i < n1; i++)
   {
       L[i] = a[(p + i)];
   }
   int[] R = new int[n2+1];
   for (j = 0; j < n2; j++)
       {
       R[j] = a[q + j];
       }
       L[n1] = Integer.MAX_VALUE;
       R[n2] = Integer.MAX_VALUE;
       i = 0;
       j = 0;
       for (int k = p; k < r; k++)
       {
           count = count + 1;
           if(L[i] <= R[j])
           {
               a[k] = L[i];
               i = i + 1;
           }
          else
           {
               a[k] = R[j];
               j = j + 1;
          }
       } 
    }
    private int[] a;
    private int p; 
    private int r;
    private int q;
    public int count = 0;
}

But this code is not working.I want to know where is the problem. Sorry for the wrong direction picture.
Updated: Here is my other code.It now dose something but not sorted right.
This is my test code below

    public static void main(String[] args) throws FileNotFoundException, IOException
    {
      int[] a = {1,4,6,2,10,7};
      MergeSorter sorter = new MergeSorter(a,0,a.length);
      sorter.sort();
      System.out.println(Arrays.toString(a));
     }        

It output [1,2,4,4,2,7] as a result.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:34:28+00:00Added an answer on May 31, 2026 at 10:34 am

    You have a couple of off-by-one errors.

    MergeSorter sorter = new MergeSorter(a,0,a.length);
    

    The last index of an array is a.length - 1.

    for (j = 0; j < n2; j++)
    {
        R[j] = a[q + j];
    }
    

    The second half to be merged runs from index q+1 to r.

    for (int k = p; k < r; k++)
    

    The last index to be filled is r, not r-1, so the condition should be k <= r.

    Further, the setting of the instance variables

    p = low;
    q = mid;
    r = high;
    

    in merge() is fishy. It doesn’t hurt here because they are set to the values they already have, but in principle it’s wrong.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

just had a walkthrough in class and the example code our teacher gave us
I am in a web scripting class and our teacher is very strict on
I had a computer science class at school and our teacher was talking about
I'm trying to understand the syntax used in STL for a class. Our teacher
Last week in our java course we were introduced the Object class and some
In our discrete mathematics course in my university, the teacher shows his students the
Our Sample Code EditText txtData = (EditText) findViewById(R.id.txtData); Button btnReadSDFile = (Button) findViewById(R.id.btnReadSDFile); btnReadSDFile.setOnClickListener(new
Our application is a Java-GWT application that uses Guice-Persist and Guice-Servlet extensively. We have
I have imported lwjgl in Eclipse. The teacher gave us a BaseWindow application to
Recently a teacher said PHP isn't a real programming language, but only gave, in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.