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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:05:12+00:00 2026-06-04T10:05:12+00:00

I have a mergesort function I am testing using JUnit. Below is one of

  • 0

I have a mergesort function I am testing using JUnit. Below is one of my test cases:

@Test
    //test using randomly generated numbers
    public void MergeSortTest002()
    {
        long seed = System.currentTimeMillis();
        Random rng = new Random(seed);
        Integer[] TestArray = new Integer[1000];
        int MAX_VALUE = Integer.MAX_VALUE;

        for(int i=0; i<1000; i++) {
            //this will generate positive and negative numbers from
            // -MAX_VALUE/2 to +MAX_VALUE/2
            Integer newNum = rng.nextInt(MAX_VALUE/2) - MAX_VALUE;
            TestArray[i] = newNum;
        }

        Integer[] correctArray = TestArray;
        Arrays.sort(correctArray);
        MergeSort.mergeSort(TestArray);

        Assert.assertArrayEquals(correctArray,TestArray);
    }

The strange thing is, even if I comment out the line where I call my mergeSort function, the test still passes.

I figured one of two things is happening: Either assertArrayEquals does not care about the order of elements (unlikely), of when I’m copying TestArray to correctArray, it’s copying by reference, and thus calling Arrays.sort on correctArray is sorting TestArray as well.

Could anyone confirm which of the two is happening, and what should be the solution? Is there a Assert that keeps order in mind, or is there a way to copy arrays by values rather than reference without writing an explicit for loop?

  • 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-04T10:05:15+00:00Added an answer on June 4, 2026 at 10:05 am

    The problem is right here:

    Integer[] correctArray = TestArray;
    

    You’re copying the reference to your array, not the contents of the array.

    To fix, change that line to:

    Integer[] correctArray = TestArray.clone();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written the following mergesort code. public class mergesort { public static int
Here is what I have so far : def mergesort[T <: Ordered[T]](elements : List[T])
I'd like to create various sorting classes (QuickSort, MergeSort, BucketSort...etc). I have a common
I'm trying to write my own Mergesort function in the same fashion as C's
I am working on a homework assignment where I have to implement a MergeSort
My problem is that I have two functions and one of the functions calls
I have a linked list implementation and I'm experimenting with both MergeSort and QuickSort
I have the following homework question: Sort the numbers 7 12 8 6 9
I have a function that I want to call from within a class method.
Hi I have really problem with merging two lists in such code like mergesort

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.