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

  • Home
  • SEARCH
  • 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 6668361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:02:00+00:00 2026-05-26T03:02:00+00:00

I get index out of bounds exception when i try to run this code

  • 0

I get index out of bounds exception when i try to run this code .

we are using two different array s left and right for merging ..

and using recursive merge sort to divide the array into individaul elements and merging them…

Here’s the algorithm from CLRS i am using :

    Merge(A, p, q, r)
    n1 ← q - p + 1
    n2 ← r - q
    create arrays L[1..n1 + 1] and R[1..n2 + 1]
    for i ← 1 to n1
    do L[i] ← A[p + i - 1]
    for j ← 1 to n2
      do R[j] ← A[q + j]
    L[n1 + 1] ← ∞
    L[n2 + 1] ← ∞
    i ← 1
    j ← 1
    for k ≤ p to r
        do if L[i] ≤ R[j]
          then A[k] ← L[i]
             i ← i + 1
          else A[k] ← R[j]
              j ← j + 1

     MergeSort(A, p, r)
     if p < r
      then q ← ⌊(p + r)/2⌋
       MergeSort(A, p, q)
       MergeSort(A, q + 1, r)
       Merge(A, p, q, r)

here’s the code:

import java.util.Arrays;
import java.util.Scanner;

public class MergeSort {
   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      System.out.println("Enter the size of array to be sorted");
      int size = input.nextInt();
      int[] A = new int[size];
      System.out.println("Enter the elements of array");
      for (int i = 0; i < A.length; i++) {
         A[i] = input.nextInt();
      }
      System.out.println("The UNSORTED array elements are" + Arrays.toString(A));
      int p = 0, r = size;
      mergeSort(A, p, r);
      System.out.println("The SORTED array elements are" + Arrays.toString(A));
   }

   public static void mergeSort(int[] A, int p, int r) {
      if (p < r) {
         int q = (p + r) / 2;
         mergeSort(A, p, q);
         mergeSort(A, q + 1, r);
         merge(A, p, q, r);
      }
   }

   public static void merge(int[] A, int p, int q, int r) {
      int n1 = q - p + 1;
      int n2 = r - q;
      int[] L = new int[n1 + 1];
      int[] R = new int[n2 + 1];
      L[n1] = Integer.MAX_VALUE;
      R[n2] = Integer.MAX_VALUE;
      for (int i = 0; i < n1; i++) {
         L[i] = A[p + i];
      }
      for (int j = 0; j < n2; j++) {
         R[j] = A[q + j + 1];
      }
      int x = 0, y = 0;
      for (int k = p; k < r; k++) {
         if (L[x] <= R[y]) {
            A[k] = L[x];
            x++;
         } else {
            A[k] = R[y];
            y++;
         }
      }
   }
}
  • 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-26T03:02:00+00:00Added an answer on May 26, 2026 at 3:02 am

    After a second but longer look, your implementation of the algorithm seems to be right, i got confused regarding your input parameters because they did not fit your zero based implementation. So maybe you do not want to call it like this:

    int p = 0, r = size;
    mergeSort(A, p, r);
    

    but rather like this:

    int p = 0, r = size - 1;
    mergeSort(A, p, r);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have stumbled upon an issue I can't figure out right now. I get
How do you get the proper index of a selected input from a set
Was looking to get peoples thoughts on keeping a Lucene index up to date
I would like to get up-to-date information on Google's index of a website, and
Given a columns' index, how can you get an Excel column name? The problem
How to get an instance's member's values? With propertyInfos there is a propertyInfo.GetValue(instance, index)
I need to perform a HTTP GET from PHP. More specifically, from within /index.php
Everything was working fine in this program I've been writing, and then out of
I think my problem is a classic one, but I can't get the right
We get a large amount of data from our clients in pdf files 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.