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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:20:00+00:00 2026-06-15T13:20:00+00:00

I am new to Java and have tried to implement mergesort in Java. However,

  • 0

I am new to Java and have tried to implement mergesort in Java. However, even after running the program several times, instead of the desired sorted output, I am getting the same user given input as the output. I would be thankful if someone could help me understand this unexpected behaviour.

import java.io.*;
import java.util.Arrays;

public class MergeSort {

    public static void main(String[] args) throws IOException {
        BufferedReader R = new BufferedReader(new InputStreamReader(System.in));
        int arraySize = Integer.parseInt(R.readLine());
        int[] inputArray = new int[arraySize];
        for (int i = 0; i < arraySize; i++) {
            inputArray[i] = Integer.parseInt(R.readLine());
        }
        mergeSort(inputArray);
        
        for (int j = 0; j < inputArray.length; j++) {
            System.out.println(inputArray[j]);
        }
    }
    
    static void mergeSort(int[] A) {
        if (A.length > 1) {
            int q = A.length / 2;
            int[] leftArray = Arrays.copyOfRange(A, 0, q);
            int[] rightArray = Arrays.copyOfRange(A, q + 1, A.length);
            mergeSort(leftArray);
            mergeSort(rightArray);
            A = merge(leftArray, rightArray);
        }
    }
    
    static int[] merge(int[] l, int[] r) {
        int totElem = l.length + r.length;
        int[] a = new int[totElem];
        int i, li, ri;
        i = li = ri = 0;
        while (i < totElem) {
            if ((li < l.length) && (ri < r.length)) {
                if (l[li] < r[ri]) {
                    a[i] = l[li];
                    i++;
                    li++;
                } else {
                    a[i] = r[ri];
                    i++;
                    ri++;
                }
            } else {
                if (li >= l.length) {
                    while (ri < r.length) {
                        a[i] = r[ri];
                        i++;
                        ri++;
                    }
                }
                if (ri >= r.length) {
                    while (li < l.length) {
                        a[i] = l[li];
                        li++;
                        i++;
                    }
                }
            }
        }
        return a;
    }
}
  • 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-06-15T13:20:02+00:00Added an answer on June 15, 2026 at 1:20 pm

    Here is a corrected version of your code:

    import java.io.*;
    import java.util.Arrays;
    
    
    public class MergeSort {
    
        public static void main(String[] args) throws IOException{
            BufferedReader R = new BufferedReader(new InputStreamReader(System.in));
            int arraySize = Integer.parseInt(R.readLine());
            int[] inputArray = new int[arraySize];
            for (int i = 0; i < arraySize; i++) {
                inputArray[i] = Integer.parseInt(R.readLine());
            }
            mergeSort(inputArray);
    
            for (int j = 0; j < inputArray.length; j++) {
                System.out.println(inputArray[j]);
            }
    
        }
    
        static void mergeSort(int[] A) {
            if (A.length > 1) {
                int q = A.length/2;
    
    //changed range of leftArray from 0-to-q to 0-to-(q-1)
                int[] leftArray = Arrays.copyOfRange(A, 0, q-1);
    //changed range of rightArray from q-to-A.length to q-to-(A.length-1)
                int[] rightArray = Arrays.copyOfRange(A,q,A.length-1);
    
                mergeSort(leftArray);
                mergeSort(rightArray);
    
                merge(A,leftArray,rightArray);
            }
        }
    
        static void merge(int[] a, int[] l, int[] r) {
            int totElem = l.length + r.length;
            //int[] a = new int[totElem];
            int i,li,ri;
            i = li = ri = 0;
            while ( i < totElem) {
                if ((li < l.length) && (ri<r.length)) {
                    if (l[li] < r[ri]) {
                        a[i] = l[li];
                        i++;
                        li++;
                    }
                    else {
                        a[i] = r[ri];
                        i++;
                        ri++;
                    }
                }
                else {
                    if (li >= l.length) {
                        while (ri < r.length) {
                            a[i] = r[ri];
                            i++;
                            ri++;
                        }
                    }
                    if (ri >= r.length) {
                        while (li < l.length) {
                            a[i] = l[li];
                            li++;
                            i++;
                        }
                    }
                }
            }
            //return a;
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Java. I just read that class variables in Java have
I'm new to Java and have gotten myself into a situation where it's evident
I am new in Java and have got as a task to find out,
Forgive me, I'm new to Java and have an extremely basic question. I have
Like lots of askers on SO, I'm relatively new to java and have attempted
Completely new to java and I have been playing around with regex in a
I am new to java and I have some problem on Casting. I have
I'm new to Java EE and have been struggling with some basic middleware concepts
I'm new to Java and I have a beginner question: NumberFormat is an abstract
I have the following code adding an ActionListener to a JTextField: chatInput.addMouseListener(new java.awt.event.MouseAdapter() {

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.