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 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

Alright, so I'm using arc4random to get a random image out of an array,
I seem to be getting an array out of bounds exception but the problem
I wanted to know how can we get the index of the array if
I wrote this code today, just out of experimentation, and I'm trying to figure
I'm getting an out of bounds exception on the calendarTable[i][j] = str; line below.
I am getting cursor index out of bounds index 0 requested: with size 0
Whenever I try to access the number of a contact I get an index
I have stumbled upon an issue I can't figure out right now. I get
When I enable (uncomment) this line of code I get a SIGABRT crash -
Please check my website out: http://www.budgie.richardcmpage.com/index.php I've tried all sorts and can't get the

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.