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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:41:07+00:00 2026-05-27T21:41:07+00:00

I have been trying to code merge sort, without creating additional arrays for keeping

  • 0

I have been trying to code merge sort, without creating additional arrays for keeping sorted parts, after few hours I can’t find the error, which causes last bit of the array to be sorted in a wrong order. Theres quite a few helper methods, I used for the debugging, I left them in.

public class Sorter2
{

    public static void toString(int [] list)
    {
        for(int i = 0; i < list.length; i++)
        {
            System.out.print(list[i]);
            if(!(i + 1 == list.length))
            {
                System.out.print(",");
            }
        }

        System.out.println("");
    }

    public static void toString(int list[], int from, int to)
    {
        for(int i = from; i <= to; i++)
        {
            System.out.print(list[i]);
            if(i + 1 <= to)
            {
                System.out.print(",");
            }
        }

        System.out.println("");
    }


    public static void insertAt(int [] list, int insert_at, int taken_from)
    {
        int to_insert = list[taken_from];
        for(int i = taken_from; i >= insert_at; i--)
        {
            if(i != insert_at)
            {
                list[i] = list[i - 1];
            }
            else
            {
                list[i] = to_insert;
            }
        }
    }

    public static void sortSegments(int [] list ,int segment_one_begin, int segment_one_end, int segment_two_begin, int segment_two_end)
    {
        toString(list, segment_one_begin, segment_two_end);
        int sorted = 0;
        for(int i = segment_two_begin; i <= segment_two_end; i++)
        {
            for(int l = segment_one_begin + sorted; l <= segment_one_end; l++)
            {
                if(list[i] <= list[l])
                {
                    insertAt(list, l, i);
                    sorted++;
                }
            }
        }
        toString(list, segment_one_begin, segment_two_end);
    }

    public static void mergeSort(int [] list, int segment_begining, int segment_end)
    {
        if(segment_end - segment_begining < 1)
        {
            return;
        }
        else
        {
            int midpoint = (segment_end + segment_begining) / 2;

            mergeSort(list, segment_begining, midpoint);
            mergeSort(list, midpoint + 1, segment_end);
            sortSegments(list, segment_begining, midpoint, midpoint + 1, segment_end);

        }

    }
    public static void mergeSort(int [] list)
    {
        mergeSort(list, 0, list.length - 1);
    }

    public static boolean isInOrder(int [] toCheck)
    {
        for(int i = 1; i < toCheck.length; i++)
        {
            if(toCheck[i] < toCheck[i - 1])
            {
                return false;
            }
        }

        return true;
    }

    public static int [] populate(int numOfItems)
    {
        int [] toReturn = new int[numOfItems];

        for(int i = 0; i < toReturn.length; i++)
        {
            toReturn[i] = (int) (Math.random() * 100 + 1);
        }

        return toReturn;
    }

    public static void main(String [] args)
    {
        int [] nums = populate(20);
        mergeSort(nums);
        toString(nums);
        System.out.println(isInOrder(nums));

    }
}
  • 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-27T21:41:07+00:00Added an answer on May 27, 2026 at 9:41 pm

    Let’s adjust your code a bit so that the tests would be reproducible, and we could see the whole process:

    import java.util.Random;
    
    public class Sorter2 {
        public static final Random RANDOM = new Random(55);
    
        public static void toString(int[] list) {
            System.out.println(Arrays.toString(list));
        }
    
        public static void toString(int list[], int from, int to) {
            System.out.print(from + "\t" + to + "\t");
            for (int i = from; i <= to; i++) {
                System.out.print(list[i]);
                if (i + 1 <= to) {
                    System.out.print(",");
                }
            }
    
            System.out.println("");
        }
    
    
        public static void insertAt(int[] list, int insert_at, int taken_from) {
            int to_insert = list[taken_from];
            for (int i = taken_from; i >= insert_at; i--) {
                if (i != insert_at) {
                    list[i] = list[i - 1];
                } else {
                    list[i] = to_insert;
                }
            }
        }
    
        public static void sortSegments(int[] list, int segment_one_begin, int segment_one_end, int segment_two_begin, int segment_two_end) {
            System.out.println("Sorter2.sortSegments("+segment_one_begin + "," + segment_one_end + "," + segment_two_begin + "," + segment_two_end + ")");
            toString(list, segment_one_begin, segment_two_end);
            int sorted = 0;
            for (int i = segment_two_begin; i <= segment_two_end; i++) {
                for (int l = segment_one_begin + sorted; l <= segment_one_end; l++) {
                    if (list[i] <= list[l]) {
                        insertAt(list, l, i);
                        sorted++;
                    }
                }
            }
            toString(list, segment_one_begin, segment_two_end);
        }
    
        public static void mergeSort(int[] list, int segment_begining, int segment_end) {
            if (segment_end - segment_begining < 1) {
                return;
            }
    
            int midpoint = (segment_end + segment_begining) / 2;
            mergeSort(list, segment_begining, midpoint);
            mergeSort(list, midpoint + 1, segment_end);
            sortSegments(list, segment_begining, midpoint, midpoint + 1, segment_end);
        }
    
        public static void mergeSort(int[] list) {
            mergeSort(list, 0, list.length - 1);
        }
    
        public static boolean isInOrder(int[] toCheck) {
            for (int i = 1; i < toCheck.length; i++) {
                if (toCheck[i] < toCheck[i - 1]) {
                    return false;
                }
            }
    
            return true;
        }
    
        public static int[] populate(int numOfItems) {
            int[] toReturn = new int[numOfItems];
    
            for (int i = 0; i < toReturn.length; i++) {
                toReturn[i] = (int) (nextRandom() * 100 + 1);
            }
    
            return toReturn;
        }
    
        private static double nextRandom() {
            return RANDOM.nextDouble();
        }
    
        public static void main(String[] args) {
            int[] nums = populate(20);
            mergeSort(nums);
            toString(nums);
            System.out.println(isInOrder(nums));
        }
    }
    

    The output is as follows:

    Sorter2.sortSegments(0,0,1,1)
    0   1   73,47
    0   1   47,73
    Sorter2.sortSegments(0,1,2,2)
    0   2   47,73,48
    0   2   47,48,73
    Sorter2.sortSegments(3,3,4,4)
    3   4   42,64
    3   4   42,64
    Sorter2.sortSegments(0,2,3,4)
    0   4   47,48,73,42,64
    0   4   42,47,48,73,64
    Sorter2.sortSegments(5,5,6,6)
    5   6   12,38
    5   6   12,38
    Sorter2.sortSegments(5,6,7,7)
    5   7   12,38,14
    5   7   12,14,38
    Sorter2.sortSegments(8,8,9,9)
    8   9   18,87
    8   9   18,87
    Sorter2.sortSegments(5,7,8,9)
    5   9   12,14,38,18,87
    5   9   12,14,18,38,87
    Sorter2.sortSegments(0,4,5,9)
    0   9   42,47,48,73,64,12,14,18,38,87
    0   9   12,42,14,18,38,47,48,64,73,87
    Sorter2.sortSegments(10,10,11,11)
    10  11  60,29
    10  11  29,60
    Sorter2.sortSegments(10,11,12,12)
    10  12  29,60,95
    10  12  29,60,95
    Sorter2.sortSegments(13,13,14,14)
    13  14  21,37
    13  14  21,37
    Sorter2.sortSegments(10,12,13,14)
    10  14  29,60,95,21,37
    10  14  21,29,37,60,95
    Sorter2.sortSegments(15,15,16,16)
    15  16  28,66
    15  16  28,66
    Sorter2.sortSegments(15,16,17,17)
    15  17  28,66,73
    15  17  28,66,73
    Sorter2.sortSegments(18,18,19,19)
    18  19  80,69
    18  19  69,80
    Sorter2.sortSegments(15,17,18,19)
    15  19  28,66,73,69,80
    15  19  28,66,69,73,80
    Sorter2.sortSegments(10,14,15,19)
    10  19  21,29,37,60,95,28,66,69,73,80
    10  19  21,28,29,37,60,95,66,69,73,80
    Sorter2.sortSegments(0,9,10,19)
    0   19  12,42,14,18,38,47,48,64,73,87,21,28,29,37,60,95,66,69,73,80
    0   19  12,21,28,29,37,42,14,18,38,47,48,64,73,87,60,95,66,69,73,80
    12,21,28,29,37,42,14,18,38,47,48,64,73,87,60,95,66,69,73,80
    false
    

    As you can see, the first problem manifests itself in the following lines:

    Sorter2.sortSegments(0,2,3,4)
    0   4   47,48,73,42,64
    0   4   42,47,48,73,64
    

    We take two ordered chunks and get an unordered chunk as a result. Put a breakpoint on line for (int i = segment_two_begin; i <= segment_two_end; i++) { and try to catch the case for Sorter2.sortSegments(0,2,3,4):

    • 42 <= 47, so we call insertAt to move 42 before 47
    • but that means 73 is now in segment_two—and believed to be in place!

    Here’s the error for you: your sortSegments does not work as advertised.

    If you think a minute about that method, you’ll find that you do not actually need nested loops: all you need is to find the necessary elements, step by step. So you’re better off with one cycle from segment_one_begin to segment_two_end, and a pointer to a current position in the second list: if an element from first list is lower than one from the second, you just skip to the new position. If it’s not, you perform the shift you need—and move a pointer.

    I’ve made a fix and it works just fine for me—so it seems is was the only error in your implementation. If you’re still stuck, please describe your problem, and I’ll try to help.

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

Sidebar

Related Questions

I have been trying to find a code snippet to do an unsharp mask
I have been trying to learn how to add testing to existing code --
I have been trying to find out why the following lines of code do
I have been trying to use the hibernate dialect for SQLite from http://code.google.com/p/hibernate-sqlite/ in
I've been trying to implement unit testing and currently have some code that does
I am trying to use the following code, which I have not been able
I have been trying to get the following code working. It's a progress bar
I have been trying to get the following code to work(everything is defined in
I have been trying to debug the below python cgi code but doesn't seems
I have been trying to execute this program on my MinGW, through Code::Blocks: #include

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.