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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:59:13+00:00 2026-06-15T09:59:13+00:00

I have written a method to merge sort an array of values. Method works

  • 0

I have written a method to merge sort an array of values. Method works perfectly but I am trying to show the number of comparisons and exchanges that have taken place. My first thought was to create static variables (int comparisons, int exchanges) and pass them into the method. however am running into problems returning them from the helper method. Is it possible to return an int[] and two ints in the same method? If not, how can I determine the number of comparisons and exchanges that have taken place?

Here are my mergeSort and merge methods:

    public static int[] mergeSort(int[] A, int comps, int exchs) {
    // Array has only 1 element
    if( A.length <= 1 ) {
        return A;
    }
    int midPoint = A.length / 2;
    // Initialize left and right arrays.
    int[] left = new int[midPoint];
    int[] right = new int[A.length - midPoint];
    System.arraycopy(A, 0, left, 0, midPoint);
    System.arraycopy(A, midPoint, right, 0, A.length - midPoint);
    //recursively sort left and right arrays
    left = mergeSort(left, comps, exchs);
    right = mergeSort(right, comps, exchs);
    System.out.println("Comparisons" + comps);
    System.out.println("Exchanges" + exchs);
    return merge(left, right, comps, exchs);

} 
private static int[] merge(int[] left, int[] right, int comps, int exchs){
    // Initialize the result array.
    int[] res = new int[left.length + right.length];
    // Initialize the array indexes.
    int leftIndex = 0; 
    int rightIndex = 0;
    int resIndex = 0; 
    // compare each element and merge results
    while(leftIndex < left.length && rightIndex < right.length){
        if(left[leftIndex] > right[rightIndex]){
            res[resIndex] = right[rightIndex];
            exchs++;
            rightIndex++;
        } else {
            res[resIndex] = left[leftIndex];
            exchs++;
            leftIndex++; 
        }
        comps++;
        resIndex++;
    }
    comps++;
    // Append remainder of left array into the result array.
    while(leftIndex < left.length){
        res[resIndex] = left[leftIndex];
        exchs++;
        leftIndex++;
        resIndex++;
    }
    comps++;
    // Append whatever is left from the right array into the result array.
    while(rightIndex < right.length) {
        res[resIndex] = right[rightIndex];
        exchs++;
        rightIndex++; 
        resIndex++;
    }
    comps++;
    return res; // want to return comparisons and exchanges to mergeSort method
}
  • 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-15T09:59:14+00:00Added an answer on June 15, 2026 at 9:59 am

    Create an object that does the sorting for you. Then it could store the comps and exchs and you can just access them with a getter method…

    public class MergeSorter {
      private int comps = 0;
      private int exchs = 0;
    
      public int[] mergeSort(int[] A) {
        comps = 0;
        exchs = 0;
        // your code
      }
      private int[] merge(int[] left, int[] right) {
        // your code
      }
    
      public int getLastComps() { return comps; }
      public int getLastExchs() { return exchs; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a method that downloads some files, and now I'm trying to
I have written method which should return string with data from select query, but
I have written a method (shown below). The project is compiled using ARC, but
I have written this method in objectiveC (for iOS but that immaterial). My app
I have written this method which given a number and a list will return
I have written a method to calculate a given percentile for a set of
i have written a method in java like this public boolean ADD(String ID,String Name,String
I have written an extension method to help with collecting crash data during error
I have written this clone method for when the parent of the Employee class
I have written an extension method for string manipulation. I'm confused what should I

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.