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 7443003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:15:47+00:00 2026-05-29T11:15:47+00:00

I am working on a homework assignment where I have to implement a MergeSort

  • 0

I am working on a homework assignment where I have to implement a MergeSort based on the author of our books psuedo-code. (Foundations of Algorithms, 4th ed by Neapolitan and Naimipour).

From the main method I am calling mergeSort with the int n being the length of the array and S[] being the array needing to be sorted. S[] is already filled with random numbers between 1 and 999. My program is crashing at the last arraycopy in merge and I am not sure why it is and I have tried debugging in netbeans. I feel like this is probably a dumb mistake that I just can’t see. Any help would be great. Thanks in advanced.

public static void mergesort (int n, int S[])
   {
      if(n>1){
         final int h=n/2, m=n-h;
         int U[] = new int[h];
         int V[] = new int[m];
         System.arraycopy(S, 0, U, 0, h);
         System.arraycopy(S, h, V, 0, m);
         mergesort(h, U);
         mergesort(m, V);
         merge(h, m, U, V, S);
      }
   }
   public static void merge(int h, int m, final int U[], final int V[], final int S[])
   {
      int i=0, j=0, k=0;
      while(i<h && j<m){
         if(U[i]<V[j]) {
            S[k] = U[i];
            i++;
         }
         else {
            S[k]=V[j];
            j++;
         }
         k++;
      }
      if(i>h)
         System.arraycopy(V, j, S, k, h+m);
      else
         System.arraycopy(U, i, S, k, h+m);
   }

EDIT: I realized I had a couple small mistakes in merge. The biggest changes were changing comparisons to work with equals and also understanding the length in arraycopy is the number of elements I want to copy. Here is my merge method. mergesort stayed the same.

   public static void merge(int h, int m, final int U[], final int V[], int S[])
   {
      int i=0, j=0, k=0;
      while(i<h && j<m){
         if(U[i]<V[j]) {
            S[k] = U[i];
            i++;
         }
         else {
            S[k]=V[j];
            j++;
         }
         k++;
      }
      if(i>=h)
         System.arraycopy(V, j, S, k, m-j);
      else
         System.arraycopy(U, i, S, k, h-i);
   }

Thanks for the help.

  • 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-29T11:15:48+00:00Added an answer on May 29, 2026 at 11:15 am

    Tested it. It as an IndexOutOfBounds.
    The reason is that the length that you give in the ArrayCopy is greater than the count off your source-Array.

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

Sidebar

Related Questions

I'm working on a homework assignment to modify code given by my professor using
I'm working on a homework assignment where we are asked to implement an evaluation
I am working on a homework assignment, and I have almost everything done except
I'm working on a homework assignment for CS1, and I almost have it finished
I am currently working on a homework assignment where I have to create a
I am working on a homework assignment, beginning C#. Have to accept input from
I'm working on a homework assignment in which I'm required to use char arrays
I'm working on a homework assignment (a project), for which one criterion is that
Working on a project at the moment and we have to implement soft deletion
I'm working on a homework assignment and I ran into a little snag. I'm

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.