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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:17:08+00:00 2026-06-07T07:17:08+00:00

Okay, I know this question doesn’t show research effort, but I’ve been going through

  • 0

Okay, I know this question doesn’t show research effort, but I’ve been going through this code so many times and I couldn’t figure out was I was doing wrong. I know there are many Mergesort implementation examples but I wanted to do it my way. Any help is appreciated, thanks.

import java.util.Scanner;
public class MergeSort
{
    public static int[] mergeSort(int[] arr)
    {
        if (arr.length > 1)
        {
            int[] arr1 = splitLeft(arr);
            int[] arr2 = splitRight(arr);
            arr1 = mergeSort(arr1);
            arr2 = mergeSort(arr2);
            return merge(arr1, arr2);
        }
        else
            return arr;
    }

    public static int[] splitLeft(int[] arr)
    {
        int middle = arr.length / 2;
        int[] newarr = new int[middle];
        for (int i = 0; i < middle; i++)
            newarr[i] = arr[i];
        return newarr;
    }

    public static int[] splitRight(int[] arr)
    {
        int middle = arr.length / 2;
        int[] newarr = new int[arr.length - middle];
        for (int i = 0; i + middle < arr.length; i++)
            newarr[i] = arr[i + middle];
        return newarr;
    }

    public static int[] merge(int[] arr1, int[] arr2)
    {       
        int[] sorted = new int[arr1.length+arr2.length];

        int i1 = 0;
        int i2 = 0;
        int i = 0;

        while (i1 < arr1.length && i2 < arr2.length)
        {
            if (arr1[i1] < arr2[i2])
            {
                sorted[i] = arr1[i1];
                i1++;
            }

            else
            {
                sorted[i] = arr2[i2];
                i2++;
            }
        i++;
        }

        while (i1 < arr1.length) 
        {
            sorted[i] = arr1[i1];
            i1++;
            i++;
        }

        while (i2 < arr2.length) 
        {
            sorted[i] = arr1[i2];
            i2++;
            i++;
        }
        return sorted;
    }

    public static int getNum(int x)
    {
        int num = (int)(Math.random()*x + 1);
        return num;
    }

    public static void printArr(int[] arr)
    {
        System.out.println();
        for (int i = 0; i < arr.length; i++)
            System.out.println(arr[i]);
    }

    static Scanner reader = new Scanner(System.in);
    public static void main(String [ ] args)
    {
        int i;

        System.out.println("Type the length of the array");
        int n = reader.nextInt();

        System.out.println("Type the range of the random numbers generator");
        int range = reader.nextInt();

        int[]arr = new int[n];

        for (i = 0; i < n; i++)
            arr[i] = getNum(range);

        printArr(arr);

        int[] sorted = new int[n];
        sorted = mergeSort(arr);

        printArr(sorted);
    }
}
  • 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-07T07:17:10+00:00Added an answer on June 7, 2026 at 7:17 am

    I think the problem is in your splitRight function. Consider this code:

    for (int i = middle; i < arr.length; i++)
        newarr[i] = arr[i];
    

    This tries to copy the ith element from arr to the ith position of newarr, but this is incorrect. For example, if the array arr has ten elements, you want to copy element 5 of arr to position 0 of newArr, element 6 of arr to position 1 of newarr, etc.

    To fix this, consider trying something like this:

    for (int i = 0; i + middle < arr.length; i++)
        newarr[i] = arr[i + middle];
    

    Hope this helps!

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

Sidebar

Related Questions

Okay, I know it is weird but when I put this code between <head
Okay, this is (probably) a very simple question, but I am afraid I know
Okay, I know that there are many question about it, but they are all
Okay, I know this is a Noob question but I cant see to get
Okay, I know this has been asked before, but the solutions to previously asked
Okay I know this is a really lame question because Starbasic is a really
okay so this is probably a soft pitch question for sombody, but I want
Okay, now this is more a rant about Linux than a question, but maybe
Okay this is probably a rookie question, but I have never done GUI programming
Why I ask this question: I know there have been a lot of questions

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.