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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:47:31+00:00 2026-06-03T21:47:31+00:00

I am learning Java and algorithms at the same time. I implemented merge sort

  • 0

I am learning Java and algorithms at the same time. I implemented merge sort in this class.

public class Sorter {
    private void merge(int [] numbers, int low, int mid, int high) {
            // create a new array that will contain the merged integers
            int[] arrIntMerged = new int[high - low + 1];

            // set indices
            int i = low, j = mid + 1, k = 0;

            // add the lesser integer into merged array
            while (i <= mid && j <= high) {
                if (numbers[i] < numbers[j]) {
                    arrIntMerged[k] = numbers[i];
                    i++;
                } else {
                    arrIntMerged[k] = numbers[j];
                    j++;
                }
                k++;
            }

            // add anything left in the left side of the array
            while (i <= mid) {
                arrIntMerged[k] = numbers[i];
                i++;
                k++;
            }

            // add anything left in the right side of the array
            while (j <= high) {
                arrIntMerged[k] = numbers[j];
                j++;
                k++;
            }

            // write this newly created array into the positions in the original array
            for (int l = 0; l < arrIntMerged.length; l++) {
                numbers[l + low] = arrIntMerged[l];
            }
        }

        // recursive implementation
        private void _mergeSort(int[] numbers, int low, int high) {
            if (low == high)
                return;
            else {
                // find midpoint while preventing overflow
                int mid = low + (high - low) / 2;
                // sort left and right side
                _mergeSort(numbers, low, mid);
                _mergeSort(numbers, mid + 1, high);
                // merge both sides
                merge(numbers, low, mid + 1, high);
            }
        }

        // friendly interface to begin merge sort
        public void mergeSort(int[] numbers) {
            _mergeSort(numbers, 0, numbers.length - 1);
        }
}

I then inspected this code in a scrapbook in Eclipse.

Sorter sorter = new Sorter();
int[] nums = {5, 6, 7, 8, 1, 2, 3, 4};
sorter.mergeSort(nums);
System.out.println(Arrays.toString(nums));

Unfortunately, standard output reads [2, 3, 4, 5, 6, 7, 8, 1], which is out of order. Why is my merge sort erring? I am pretty sure of my boundary conditions in _mergeSort, so I suspect my merge function is awry.

  • 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-03T21:47:33+00:00Added an answer on June 3, 2026 at 9:47 pm

    Your problem comes from the following assignment in merge:

    j = mid + 1
    

    mid is already the index to the first number on the right side of the merge, this increment is making the rest of your merge logic start at the wrong array position.

    Because it looks like you are learning, I won’t spoil your experience by posting the actual code changes needed, but here a hint: check all the places where you compare things against the mid value.

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

Sidebar

Related Questions

At school we are currently learning sorting algorithms in Java and I got for
Learning Java I was sometimes taught to use the private access modifier so as
Well..I am learning java now and I am curious to know will this yield
I am learning Java and I want to make my class into an observable
Im learning Java and having a problem with ArrayList. Firstly I have a class
While learning Java I stumble upon this error quite often. It goes like this:
I'm learning Java and I have no idea how to do this. I dragged
so I'm building this collaborative filtering system using Weka's machine learning library JAVA API...
just learning Java and I know this may sound stupid but I have to
Good morning I am learning Java and trying to understand the access to class

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.